Explain the structure of Java program.
Structure of a Java Program (Easy Explanation)
A Java program is organized into packages, classes, and methods. The most common starting point is a class with a main method. Below is the typical structure used in BTech CSE courses and beginner-friendly Java projects.
Basic Structure of a Java Program
// 1) Package declaration (optional but recommended)
package com.example.app;
// 2) Import statements (optional)
import java.util.Scanner;
/**
* 3) Class declaration
* File name must match this public class name: HelloWorld.java
*/
public class HelloWorld {
// 4) Fields / Variables (state)
private String message = "Hello, Java!";
// 5) Constructor (initializes objects)
public HelloWorld() {
// initialization code (optional)
}
// 6) Methods (behavior)
public void printMessage() {
System.out.println(message);
}
// 7) Entry point: main method (program starts here)
public static void main(String[] args) {
HelloWorld app = new HelloWorld();
app.printMessage();
// Example of using an imported class
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Welcome, " + name + "!");
sc.close();
}
}
/*
8) (Optional) Another non-public top-level class in the same file
Note: Only one public class is allowed per .java file.
*/
class Helper {
// helper code here
}
Key Components Explained
- Package declaration: Groups related classes. Must be the first non-comment line. Its folder path must match the package name (e.g.,
com/example/app). - Import statements: Bring classes from other packages into scope (e.g.,
import java.util.*;). Imports are optional if you use fully qualified names. - Class: The main building block. A file can have multiple classes, but only one can be
publicand the file name must match that public class. - Fields (variables): Store data for the class. They have types and optional access modifiers (
private,public,protected). - Constructor: Special method that runs when you create an object using
new. It has the same name as the class and no return type. - Methods: Actions the class can perform. They may accept parameters and return values.
- Main method: The entry point for standalone programs:
public: Accessible by the JVM from anywhere.static: Runs without creating an object first.void: Returns no value.String[] args: Command-line arguments.
- Comments: Document code.
// single-line/* multi-line *//** Javadoc */for API documentation.
- Access modifiers: Control visibility:
public(everywhere)private(within the class)protected(package + subclasses)- default/package-private (no keyword; same package)
File Naming and Folder Structure
- The file name must match the public class name (e.g.,
HelloWorld.java). - Package name determines directory structure:
- Package:
package com.example.app; - Path:
com/example/app/HelloWorld.java
- Package:
- Only one public class per file; other top-level classes must be non-public.
Compilation and Execution (Command Line)
// From the directory that contains the 'com' folder: javac com/example/app/HelloWorld.java // compile java com.example.app.HelloWorld // run (use the fully qualified class name)
Common Variations You May See
- Interfaces: Define method contracts without implementation.
- Enums: Define fixed sets of constants.
- Records (modern Java): Compact classes for immutable data.
- Static imports: Import static members to use without class names.
- Multiple classes in one file: Only one can be public; others have default access.
Quick Tips for Students
- Start with a single public class containing
main. - Keep class names in PascalCase and methods/variables in camelCase.
- Match file name with the public class name exactly (case-sensitive).
- Organize code into packages for larger projects.
- Use comments and meaningful names to improve readability.
