Java Source File:

Java Source File:
1.    A Java program can contain multiple classes
2.    Only one class in a java program can be declared as public
3.    If there is a public class, the name of the public class and name of the program must be matched, otherwise it will give compile time error.
4.    If there is no public  class then we can use any name for the Java Program
Example 1:
class A
{
}
class B
{
}
class C
{
}
Since there is no public class, the name of file can be anything like A.java, B.java, Himanshu.java

Example 2:
class A
{
}
public class B
{
}
class C
{
}
File name has to be B.java otherwise it will give compile time error “Class B is public, should be declared in a file name B.java”
Example 3:
public class A
{
}
public class B
{
}
class C
{
}

Compile time error, if File name is A.java then error “Class B is public, should be declared in a file name B.java” is displayed. If file name is B.java then it will give compile time error “Class A is public, should be declared in a file name B.java”