Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Source code file, multiple class definitions

 
Anneke Perelaer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic