aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Referencing classes in other files. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Referencing classes in other files." Watch "Referencing classes in other files." New topic
Author

Referencing classes in other files.

Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 178
Hi guys.

I believe I read in K&B that a package statement in a file applies to all classes in that file. As such, I am wondering why this doesn't compile:





Test class in file Test.java
Test2 class in file Test2.java
SameFile class in Test2.java

I would have thought the package statement in Test2.java applies to all classes in that file, as I said I'm quite sure I've read that in K&B. But instantiating the SameFile class doesn't work from class Test.

Am I missing something? Is it impossible to reference a class which is located in another file, if that class is not marked public (and its classname therefor does not match the name of the file) ?


// Andreas
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3865
    
    1

It worked OK when I just tried it. Maybe you've got something else going wrong, like the directory structure.

(Note that there's no need to give the full name of the class: new SameFile() would do. But that doesn't matter).
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 178
Weird... here's "how" I try to compile it:

D:\javatest\nametest>dir
Volume in drive D is Tools
Volume Serial Number is 46C4-0D93

Directory of D:\javatest\nametest

2011-06-08 15:57 <DIR> .
2011-06-08 15:57 <DIR> ..
2011-06-08 16:01 120 Test.java
2011-06-08 16:00 152 Test2.java
2 File(s) 272 bytes
2 Dir(s) 42 728 591 360 bytes free

D:\javatest\nametest>javac -cp .. Test.java
Test.java:5: cannot find symbol
symbol : class SameFile
location: package nametest
new nametest.SameFile();
^
1 error

D:\javatest\nametest>


While writing this I found my problem...

It seems to be that I had to compile the Test2.java file first, once that is done and the SameFile.class file exists, compiling Test.java works.

I'm still a bit puzzled by this requirement though, was that something you had to do too? I've never had to compile a different source-file before hand, in order to use a class in it.

// Andreas
Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 111
Hi Andreas,

I was intrigued by your example and made some further tests why the compiler needs class SameFile to be compiled in order to be found.

I tried to make SameFile pulbic(removed class Test2 too) and changed the file name to SameFile and now it compiles even if the class SameFile is not compiled.

I suppose that files that are used should be either compiled or public classes(still not compiled - source files) - but this is just my presumption(if someone can elaborate on the matter I would be really glad).
I think that it would be time consuming for the compiler to check all source files for the class that it is looking for. On the other hand, after compilation of Test2.java file, we get 2 class files and it is much easier for the compiler to find whatever it is looking for.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Referencing classes in other files.
 
Similar Threads
Class access question
Regarding Package
Compiling two public scope java classes in a .java file
Importing Classes in default package
classpath setting--Held up