It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Source code file, multiple class definitions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Source code file, multiple class definitions" Watch "Source code file, multiple class definitions" New topic
Author

Source code file, multiple class definitions

Anneke Perelaer
Greenhorn

Joined: Jun 18, 2003
Posts: 4
Hello,
I'm studying for the SCJP exam and I use the book of Kathy Sierra and Bert Bates (really great book by the way!).
It is stated there on page 61 that a source code file can contain multiple class definitions, that there can be only one public class in there and that the name of the file must match the name of the public class. So far so good, clear.
But what if there is no public class in the file (use of only default access), can the file have multiple class definitions in this case? I assume not, but I want to be sure about this.
Already thanks for any reactions, Anneke
[ June 18, 2003: Message edited by: Anneke Perelaer ]
Malcolm Featonby
Greenhorn

Joined: Jun 18, 2003
Posts: 14
Hi Anneke,
The key here is public class. A Public class must be in a source file with the same name as the class itself and there can only be one public class (excluding inner classes) in the source file.
However there can be any number of classes in the file that are not public. Further to this a package class does not have to be held in a source file with the same name as the class. (i.e. classes X, Y and Z can be held in file ABC.java and will compile fine provided that X, Y and Z are not public.)
/* A Class */
//public
class ABC {
public ABC() {
System.out.println("ABC Constructor");
}
}
/* Another Class */
//public
class DEF {
/* Local Variables */
ABC myABC;
DEF() {
myABC = new ABC();
System.out.println("DEF Constructor");
}
public static void main(String[] args) {
DEF thisClass = new DEF();
}
}
(Copy and paste the above into notepad (or whatever) save it as whatever you like .java and then compile it. It will work fine. Then uncomment the public in front of ABC class and see what happens. Then uncomment the public before DEF and see what happens. Then rename the file.)
I found when I did the exam that the best way to get answers to questions like these is to write a simple sample class to see what happens.
Good luck for the exam.
Malcolm
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Originally posted by Anneke Perelaer:

But what if there is no public class in the file (use of only default access), can the file have multiple class definitions in this case? I assume not, but I want to be sure about this.
Already thanks for any reactions, Anneke
[ June 18, 2003: Message edited by: Anneke Perelaer ]

Anneke,
Yes,you can have multiple class definitions in a file & you can give any name you want...given in kathy & berts book.
class B{}
class V{}
I saved the above classes in BV.java .It compiles fine.
Veena


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
 
Don't get me started about those stupid light bulbs.
 
subject: Source code file, multiple class definitions
 
Similar Threads
class name and file name should be same??
what is wrong with this?
ClassNames
name of java file
class As A Data Type