Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two public classes in a file

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
jafarali
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic