• 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Source code file, multiple class definitions

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic