[1125] in java-interest

home help back first fref pref prev next nref lref last post

Re: (Fwd) Re: mutually dependent public classes

daemon@ATHENA.MIT.EDU (Jonathan Payne)
Sat Aug 19 20:10:12 1995

Date: Sat, 19 Aug 1995 11:32:00 -0700
From: jpayne@starwave.com (Jonathan Payne)
To: pambrose@weblogic.com
Cc: jsw@cs.brown.edu, java-interest@java.Eng.Sun.COM
In-Reply-To: <199508190625.XAA13785@blob.best.net> (pambrose@weblogic.com)

[I hope it's OK to forward this to the net, but why wouldn't it be?]

In summary, mutually dependent classes work just fine for me, the
compiler has no problem finding them, even when they are in different
packages.  See below for "proof" :-)  So if it's not working for you,
then there's probably something wrong with your CLASSPATH.  Or, you're
doing things in a different way than I ever have done them.  If you
still can't get it work to work, send me mail and I'll help sort it
out.

This is one of the features of Java that makes it such a pleasure to
use.  No forward declarations necessary *at all*!  Everything's
declared just once, in one place, in *ANY* order.

[Phew! Geeky/dork comment deleted just in time ...]

Read below ...

> Comments: Authenticated sender is <pambrose@mail.best.com>
> From: "Paul Ambrose" <pambrose@weblogic.com>
> Date:          Fri, 18 Aug 1995 23:25:11 +0000
> Priority: normal
> X-Mailer: Pegasus Mail for Windows (v2.01)
> Content-Type: text
> Content-Length: 1313
> 
> Hi Jonathan,
> 
> Do you think the following posting is true?
> Thus far, it has been true for me, but you said something
> earlier this week in a java-interest posting indicating that
> mutually dependent classes should have no problem.
> 
> Paul
> 
> ------- Forwarded Message Follows -------
> From:          Jeff White <jsw@cs.brown.edu>
> Date:          Fri, 18 Aug 1995 19:56:07 -0400
> To:            java-interest@java.sun.com
> Subject:       Re: mutually dependent public classes
> Reply-to:      jsw@cs.brown.edu
> 
> 
> I don't think this issue has been completely cleared.  Once again, the
> problem is what if two classes both need to know about each other.
> For example consider two public classes A and B.  Neither file can be
> compiled because the other .class file doesnt' exist yet.  
> 
> Someone suggested using interfaces.  Is this the only solution? 
> 
> thanks,
> jeff

This is just not true.  It works fine, you don't need interfaces or
anything.  If *anybody* is still having a problem, send me mail and I
will help sort it out.

Here's A.java:

    class A {
	B b;

	A() {
	    b = new B();
	}
    }

Here's B.java:

    class B {
	A a;

	B() {
	}

	void setA(A a) {
	    this.a = a;
	}
    }

A depends on B, and B depends on A:

    bash$ ls [AB]*
    A.java   B.java
    bash$ unset CLASSPATH
    bash$ echo $CLASSPATH

    bash$ 
    bash$ javac A.java
    bash$ ls [AB]*
    A.class   A.java    B.class   B.java
    bash$

Here's another example using packages:

Here's class A, similar except now it is public because B needs to
access it from a different package:

    import test.*;

    public class A {
	B b;

	public A() {
	    b = new B();
	}
    }

Same with B, except it's now in package test.

    package test;

    import A;

    public class B {
	A a;

	public B() {
	}

	void setA(A a) {
	    this.a = a;
	}
    }

Now I will compile A.java, and you will see it finds test/B.java by
itself, and then I will remove everything and compile test/B.java, and
it finds A.java with no problem.  And then I ran the exact same test
on my NT machine, and it also worked just fine.

    bash$ echo $CLASSPATH

    bash$ ls A.* test
    A.java

    test:
    B.java
    bash$ javac A.java
    bash$ ls A.* test
    A.class   A.java

    test:
    B.class   B.java
    bash$ rm *.class test/*.class
    bash$ javac B.java
    javac: can't read: B.java
    bash$ javac test/B.java
    bash$ ls A.* test
    A.class   A.java

    test:
    B.class   B.java
    bash$ 

-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post