• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Q of Khalid's inner class example

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, John.
I tried what you did and the code still won't compile.
 
John Paverd
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Claire
Here is what I did:

[ December 04, 2002: Message edited by: John Paverd ]
 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John, thanks a lot, I'll study the links you provided.
 
reply
    Bookmark Topic Watch Topic
  • New Topic