Java Source File Declaration Rules

I am a Tech Enthusiast having 13+ years of experience in ๐๐ as a ๐๐จ๐ง๐ฌ๐ฎ๐ฅ๐ญ๐๐ง๐ญ, ๐๐จ๐ซ๐ฉ๐จ๐ซ๐๐ญ๐ ๐๐ซ๐๐ข๐ง๐๐ซ, ๐๐๐ง๐ญ๐จ๐ซ, with 12+ years in training and mentoring in ๐๐จ๐๐ญ๐ฐ๐๐ซ๐ ๐๐ง๐ ๐ข๐ง๐๐๐ซ๐ข๐ง๐ , ๐๐๐ญ๐ ๐๐ง๐ ๐ข๐ง๐๐๐ซ๐ข๐ง๐ , ๐๐๐ฌ๐ญ ๐๐ฎ๐ญ๐จ๐ฆ๐๐ญ๐ข๐จ๐ง ๐๐ง๐ ๐๐๐ญ๐ ๐๐๐ข๐๐ง๐๐. I have ๐๐๐๐๐๐๐ ๐๐๐๐ ๐๐๐๐ 10,000+ ๐ฐ๐ป ๐ท๐๐๐๐๐๐๐๐๐๐๐๐ and ๐๐๐๐ ๐๐๐๐๐ ๐๐๐๐ ๐๐๐๐ 500+ ๐๐๐๐๐๐๐๐ ๐๐๐๐๐๐๐๐ in the areas of ๐๐จ๐๐ญ๐ฐ๐๐ซ๐ ๐๐๐ฏ๐๐ฅ๐จ๐ฉ๐ฆ๐๐ง๐ญ, ๐๐๐ญ๐ ๐๐ง๐ ๐ข๐ง๐๐๐ซ๐ข๐ง๐ , ๐๐ฅ๐จ๐ฎ๐, ๐๐๐ญ๐ ๐๐ง๐๐ฅ๐ฒ๐ฌ๐ข๐ฌ, ๐๐๐ญ๐ ๐๐ข๐ฌ๐ฎ๐๐ฅ๐ข๐ณ๐๐ญ๐ข๐จ๐ง๐ฌ, ๐๐ซ๐ญ๐ข๐๐ข๐๐ข๐๐ฅ ๐๐ง๐ญ๐๐ฅ๐ฅ๐ข๐ ๐๐ง๐๐ ๐๐ง๐ ๐๐๐๐ก๐ข๐ง๐ ๐๐๐๐ซ๐ง๐ข๐ง๐ . I am interested in ๐ฐ๐ซ๐ข๐ญ๐ข๐ง๐ ๐๐ฅ๐จ๐ ๐ฌ, ๐ฌ๐ก๐๐ซ๐ข๐ง๐ ๐ญ๐๐๐ก๐ง๐ข๐๐๐ฅ ๐ค๐ง๐จ๐ฐ๐ฅ๐๐๐ ๐, ๐ฌ๐จ๐ฅ๐ฏ๐ข๐ง๐ ๐ญ๐๐๐ก๐ง๐ข๐๐๐ฅ ๐ข๐ฌ๐ฌ๐ฎ๐๐ฌ, ๐ซ๐๐๐๐ข๐ง๐ ๐๐ง๐ ๐ฅ๐๐๐ซ๐ง๐ข๐ง๐ new subjects.
A Java source file has specific rules that must be followed for its structure, naming, and declaration to ensure correct compilation and execution.
File Naming Rules
The Java source file name must match the public class name (if a public class exists).
โ Correct:
public class HelloWorld { }โ Incorrect:
public class Test { }File name:
HelloWorld.java(Mismatch, will cause an error)
Java source file must have the
.javaextension.โ Example:
MyProgram.javaโ Wrong:
MyProgram.txt
Package Declaration Rules
If the file belongs to a package, the
packagestatement must be the first statement in the file.โ Correct:
package com.example; public class MyClass { }โ Incorrect:
public class MyClass { } package com.example; // Compilation Error: Package statement must be first
Import Statement Rules
Imports must be placed after the package declaration (if any) and before the class declaration.
โ Correct:
package mypackage; import java.util.Scanner; public class MyClass { }โ Incorrect:
import java.util.Scanner; package mypackage; // โ Compilation Error: Package statement must come firstWildcard imports (
*) are allowed but not recommended for efficiency.โ Correct:
javaCopyEditimport java.util.*; // Imports all classes in java.utilโ Bad Practice:
javaCopyEditimport java.util.ArrayList; import java.util.List;Instead, use:
javaCopyEditimport java.util.*;
Class Declaration Rules
A Java file can have multiple classes, but only one public class.
โ Correct:
javaCopyEditpublic class Main { } // Public class (must match filename) class Helper { } // Non-public classโ Incorrect:
javaCopyEditpublic class Main { } public class Helper { } // โ Compilation Error: Only one public class allowed
If a public class exists, the file name must match the public class name.
โ File:
Main.javapublic class Main { }โ File:
Helper.javapublic class Main { } // โ Compilation Error: Filename must be "Main.java"
Method Declaration Rules
The
mainmethod must be inside a class.โ Correct:
public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } }โ Incorrect:
public static void main(String[] args) { // โ Compilation Error: No enclosing class System.out.println("Hello, World!"); }
The
mainmethod must have the exact signature.โ Correct:
public static void main(String[] args) { }โ Incorrect:
static void main(String[] args) { } // โ Compilation Error: Must be public
Interface and Enum Declaration Rules
- A Java file can contain multiple interfaces and enums.
โ Correct:
javaCopyEditinterface MyInterface { } interface AnotherInterface { }โ Enums can also be declared within a file:
javaCopyEditenum Status { ACTIVE, INACTIVE }
- A Java file can contain a mix of classes, interfaces, and enums.
โ Example:
javaCopyEditpublic class MyClass { } interface MyInterface { } enum MyEnum { ONE, TWO }
Access Modifiers in Java Files
| Modifier | Applicable to | Scope |
public | Classes, Methods, Variables | Accessible from anywhere |
private | Methods, Variables | Accessible only within the class |
protected | Methods, Variables | Accessible within the package & subclasses |
| No Modifier (default) | Classes, Methods, Variables | Accessible only within the package |
Java Source File Structure (Best Practice)
โ Correct Java Source File Order
javaCopyEdit// 1๏ธโฃ Package declaration (optional)
package com.example;
// 2๏ธโฃ Import statements (if needed)
import java.util.*;
// 3๏ธโฃ Public class declaration (if required)
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
// 4๏ธโฃ Other classes, interfaces, enums (if needed)
class Helper { }
interface MyInterface { }
enum Status { ACTIVE, INACTIVE }
Special Cases
โ Can we have a Java file without a class?
โ No, Java requires at least one class.
Example (Invalid)
javaCopyEditSystem.out.println("Hello, World!"); // โ Compilation Error: Not inside a class
Summary: Java Source File Declaration Rules
| Rule | Description |
| File Name Rule | Must match the public class name (if any). |
| Package Rule | Must be the first statement in the file (if present). |
| Import Rule | Must be placed after the package and before the class. |
| Class Rule | Only one public class per file, but multiple non-public classes are allowed. |
| Method Rule | The main method must have public static void main(String[] args). |
| Interface/Enum Rule | Multiple interfaces and enums are allowed. |
| Access Modifier Rule | Only public and default (package-private) classes are allowed at the top level. |


