File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2 public classes in one file??

 
sonir shah
Ranch Hand
Posts: 435
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only one of the top-level classes (here TestClass) can be public but inner classes can be public at will.
HIH
 
Paul Salerno
Ranch Hand
Posts: 172
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
mark stone
Ranch Hand
Posts: 417
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic