wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q of Khalid's inner class example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q of Khalid Watch "Q of Khalid New topic
Author

Q of Khalid's inner class example

Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57

Explanation: A client can use the import statement to provide a shortcut for the names of nested top-level classes and interfaces.
But "Client1.java" won't compile: "Client1.java:2: package TopLevelClass does not exist".
Is the explanation wrong?
[ Jess added a line break so the page isn't so wide ]
[ December 04, 2002: Message edited by: Jessica Sant ]
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
I got the code to compile by putting both classes in a package called test, and changing the import statement in Client1.java to
import test.TopLevelClass.*;
I'm not sure why the import doesn't work when the classes are in the default package.


SCJP 1.4
Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57
Thanks, John.
I tried what you did and the code still won't compile.
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
Claire
Here is what I did:

[ December 04, 2002: Message edited by: John Paverd ]
Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57
Thanks a lot, John.
But if you compile this two program separately, the "Client1.java" won't compile, this is another question I felt confused.
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
Originally posted by Claire Yang:
But if you compile this two program separately, the "Client1.java" won't compile

I think that the compiler needs to load the TopLevelClass.class in order to compile Client1.java, and it can't find it in the classpath.
Try to compile the source files from the directory that contains the package directory. e.g.
My current directory is the one that contains the test directory (test is the package that the classes are in):
javac test\TopLevelClass.java
javac test\Client1.java
This worked for me (I'm using java 1.4.1). If that still doesn't work, you could try setting your classpath, or using javac -classpath
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I think the problem when trying to compile from the default package is that any import statement like:

will be interpreted as the name of the non-existing packages: "TopLevelClass" , "TopLevelClass.NestedTopLevelClass".
When compiling from the same directory that contains the java files, and no package sentences , the compiler error says:

Client1.java:2: package TopLevelClass.NestedTopLevelClass does not exist
import TopLevelClass.NestedTopLevelClass.*;

Compiling from the parent of "test" and the corresponding "package test;" sentences in place is ok: there is a test package and we are compiling from its parent directory.


SCJP2. Please Indent your code using UBB Code
Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57
Thank you very much, John and Jose. Your explanations resolve my original confusion.
But I'm still confused about the question invoked by John : why the two programs can be compiled in the test directory with one line like:
test>javac TopLevelClass.java Client1.java
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
Originally posted by Claire Yang:
why the two programs can be compiled in the test directory with one line like:
test>javac TopLevelClass.java Client1.java

Mastering the Java CLASSPATH
Sun's javac "man" page
Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57
John, thanks a lot, I'll study the links you provided.
 
 
subject: Q of Khalid's inner class example