Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Compiling two public scope java classes in a .java file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Compiling two public scope java classes in a .java file" Watch "Compiling two public scope java classes in a .java file" New topic
Author

Compiling two public scope java classes in a .java file

Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, If I have two default scope classes like(class Test{} and class Test2{}) in a file named it with Test.java or Test2.java, it will compile and why it is not compiling two classes(public class Test{} and public class Test2{}) in a file named it with Test.java or Test2.java, will not compile. Thankyou.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37993
    
  22
Most compilers have a rule that the name of a public class and the name of the file must be the same (plus .java). You can't get two classes with different names in a file and still have the file with the same name as both. You can't have two classes with the same fully qualified name, so you need two files.
That rule only applies when the classes have public access.
 
jQuery in Action, 2nd edition
 
subject: Compiling two public scope java classes in a .java file
 
Similar Threads
Importing Classes in default package
Error in Package
What name to use to save files; what classes are allowed?
basic packages question
Referencing classes in other files.