
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. |
About the author
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.
Comments
Join the discussionNo comments yet. Be the first to comment.


