aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 2 public classes in one file?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "2 public classes in one file??" Watch "2 public classes in one file??" New topic
Author

2 public classes in one file??

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435

I tried with the above code and it just compiled and ran successfully.
Shouldn't it give a compile error because there are 2 public classes in one file.???

Sonir
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
only one of the top-level classes (here TestClass) can be public but inner classes can be public at will.
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
Valentin,
Could you clarify the term "file" in this case where you are only permitted to have one public top level non inner class definition per source file, where this name matches the name of the "source file"
I've also read that one is not required to have a public class in a source file
Furthermore, top level classes can only be public or default. Not sure what this one means ....
TIA
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Paul Salerno:
Valentin,
Could you clarify the term "file" in this case where you are only permitted to have one public top level non inner class definition per source file, where this name matches the name of the "source file"
I've also read that one is not required to have a public class in a source file
Furthermore, top level classes can only be public or default. Not sure what this one means ....
TIA


You can have as many top-level classes in a file as you like, however, at most ONE can be labeled public. The name of the file (compilation unit) must be the name of the public class with a dot-java extension (MyClassFile.java).
If you don't have an explicit access modifier for a class (no private , protected, or public) then it has default access. A top level class can only have one of these two modifiers : public, or default (no explicit access modifier).
Rob


Rob
SCJP 1.4
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
there is only one public Top level class and i guess your java source file was TestClass.java. right ?
you have another public class but that is an inner class. so we should be fine. The main method of class TestClass would be called for running by the JVM.

Originally posted by sonir shah:

I tried with the above code and it just compiled and ran successfully.
Shouldn't it give a compile error because there are 2 public classes in one file.???

Sonir
 
wood burning stoves
 
subject: 2 public classes in one file??