Declaration and Access Modifier

Java Source File Structure — Complete Guide

Java Source File Structure

A single .java file can hold several classes — but there are strict rules about how many of them can be public, and how that affects the file name. Get this wrong and you'll hit a compile-time error before your code even runs. This post walks through the rules with three quick test cases.

The Core Rule

A Java program can contain any number of classes, but at most one class can be declared as public. If there is a public class, the name of the program and the name of the public class must match; otherwise we will get a compile time error.

Case 1 — No Public Class

If there is no public class then we can use any name and there are no restrictions.

class A{
}
class B{
}
class C{
}
class D{
}

Save the program as any ClassName.java.

Case 2 — One Public Class

If class B is public, then the name of the program should be B.java; otherwise we will get a compile time error saying class B is public, should be declared in a file named B.java.

class A{
}
public class B{
}
class C{
}
class D{
}

Case 3 — Two Public Classes

If classes B and C are both declared as public, and the name of the program is B.java, then we will get a compile time error saying class C is public, should be declared in a file named C.java.

class A{
}
public class B{
}
public class C{
}
class D{
}

Notes

  • Whenever compiling a Java program, a separate .class file is generated for every class present in the program.
  • We compile a Java program (a .java source file), but what we actually run is the compiled .class file.
  • Whenever executing a Java class, the corresponding class's main method is executed.
  • If the class doesn't contain a main method, we get a runtime exception: NoSuchMethodError: main.
  • If the corresponding .class file is not available, we get a runtime exception: NoClassDefFoundError.
  • It is not recommended to declare multiple classes in a single source file. It is highly recommended to declare only one class per source file, with the program's file name matching the class name — this improves the readability and maintainability of the code.

Putting It Together — An Example

class A{
    public static void main(String... args){
        System.out.print("hello");
    }
}
class B{
    public static void main(String... args){
        System.out.print("Hi");
    }
}
class C{
    public static void main(String... args){
        System.out.print("Bye");
    }
}
class D{
}
Compiling and running classes A, B, C, D
java A
output: hello

java B
output: Hi

java C
output: Bye

java D
Runtime Error: NoSuchMethodError: main

java Test
Runtime Error: NoClassDefFoundError: Test

Conclusion

One file, any number of classes — but never more than one public class, and whichever class is public dictates the file name. Every class still gets its own .class file after compiling, and you can run any of them independently, as long as it has its own main method. Skip the main method and you'll hit NoSuchMethodError: main; try to run a class that was never compiled, and you'll hit NoClassDefFoundError instead.

1 Comments

Anonymous said…
I read this it's very helpful..