Java Program Structure
The Java Program Structure defines the mandatory and optional components of a Java source file and explains how the JVM identifies and executes a program. This topic is fundamental for beginners, real-time development, and interviews.
Basic Java Program Structure
A Java program is written inside a class and executed starting from the main() method.
Sample Java Program
package com.example.demo; // 1. Package statement
import java.util.Scanner; // 2. Import statement
public class HelloWorld { // 3. Class declaration
static int count = 10; // 4. Static variable
public static void main(String[] args) { // 5. Main method
System.out.println("Hello Java");
}
void display() { // 6. Instance method
System.out.println("Display method");
}
}
1. Package Statement
- Declares the namespace of the class
- Groups related classes
- Must be the first line in the source file
Syntax:
package packageName;
Why it matters:
Avoids class name conflicts and improves project organization.
Example:
package com.softwaretips4u.corejava;
2. Import Statement
- Allows access to predefined or user-defined classes
- Comes after the package statement
- Eliminates the need for fully qualified class names
Syntax:
import java.util.List;
Why it matters:
Simplifies code readability.
Example:
- Without import → java.util.Scanner
- With import → Scanner
3. Class Declaration
- Every Java program must have at least one class
- Class name must match the file name
- Contains variables, methods, constructors, blocks
Syntax:
class ClassName {
}
Rule:
If the class is public, file name must be the same.
4. Variables (Members of Class)
a) Instance Variables
- Belong to object
- Stored in heap memory
b) Static Variables
- Belong to class
- Shared across all objects
Example:
int id; // instance variable
static int count; // static variable
5. Main Method (Entry Point)
The JVM starts execution from the main() method.
Syntax (must be exact):
public static void main(String[] args)
Explanation:
- public → JVM must access it
- static → JVM calls it without object
- void → Returns nothing
- String[] args → Command-line arguments
Why it matters:
Without main(), the program cannot run (except in frameworks).
6. Methods
- Contain business logic
- Can be static or non-static
- Improve code reuse and readability
Example:
void calculateTotal() {
// logic
}
7. Comments
Types of Comments
- Single-line → //
- Multi-line → /* */
- Documentation → /** */
Why it matters:
Improves code readability and maintainability.
Order of Components in Java Program
Correct order inside a .java file:
- Package statement
- Import statements
- Class declaration
- Variables
- Constructors
- Methods
- Main method (can appear anywhere inside class)
Important Rules (Interview Focus)
- Only one public class per file
- File name must match public class name
- Main method signature must be exact
- Code outside class is not allowed
- Package and import statements are optional
Common Mistakes by Beginners
- Incorrect main() method signature
- File name mismatch with class name
- Writing code outside class
- Forgetting static in main()
- Wrong package placement
Summary Table
| Component | Purpose |
|---|---|
| Package | Organizes classes |
| Import | Access external classes |
| Class | Blueprint of object |
| Variables | Store data |
| Methods | Define behavior |
| Main Method | Program execution start |
| Comments | Documentation |
Interview-Ready Answers
Short answer:
A Java program consists of package, import statements, class declaration, variables, methods, and the main method as the entry point.
Detailed answer:
Java programs are structured inside classes. Optional package and import statements come first, followed by class definition containing variables and methods. Execution starts from the public static void main(String[] args) method.
Key Takeaway
A correct Java program structure ensures successful compilation, execution, and maintainability. Understanding this is mandatory before moving to advanced Java concepts.