0
class First    
{    
public static void main(String[] args)    
{    
System.out.println("File found");    
}    
}

(File saved with First.java)
javac First.java ---(no error)
java First --- no error
then I change name of class to "class Second"
javac First.java --- (no error)
java Second --- no error
but If I add public and change the name of class to "public class Second"
then there is a compile time error that file should be saved with the same name as of
public class.

2 Answers2

0

file name that contains a public class should be set to the name of the public class. And a file cannot have two public classes (same reason).

Byungjoon Lee
  • 913
  • 6
  • 18
0

The name of the file only matters if the class is public, in which case the file MUST have the same name as the class (class First must be in First.java).

If the class is not public, the file name has no importance.

Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
  • But what is the reason behind that. What does JVM do in these cases, if I make it 'public' with file name different. And without 'public' file name different. – user3436422 Aug 27 '14 at 07:58
  • For more information, read [these answers](http://stackoverflow.com/questions/2134784/why-filename-in-java-should-be-same-as-class-name). – Gabriel Negut Aug 27 '14 at 08:00