wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Two public classes in a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Two public classes in a file" Watch "Two public classes in a file" New topic
Author

Two public classes in a file

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Can a file hold two public classes?
//file name test.java
public class test
{}
public class test1
{}
This gives a compilation error:
"test.java:3: Public class test1 must be defined in a file called "test1.java".
I remember reading a posting in javaranch that a file can contain
2 public classes.
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
In plain vanilla Sun JDK, at most ONE package level (i.e., non-nested top level) class in a file can be <CODE>public</CODE>


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
jafarali
Ranch Hand

Joined: Jul 15, 2000
Posts: 37
class file for public class should be named same.
eg:
public class fun {
}
should be written in 'fun.java'
and you can't have duplicate class names
think you can't have
public class joke {
}
inisde 'fun.java' it should be inside 'joke.java'
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Two public classes in a file
 
Similar Threads
Package Compilation
Compiling two public scope java classes in a .java file
inheritence
A question about the java programming rule!
What name to use to save files; what classes are allowed?