Java Program Structure – Complete Guide with Examples and Explanation


Learn the complete structure of a Java program, including classes, methods, main function, package declaration, and how to write a well-structured Java program for beginners.

Java Program Structure – Complete Detailed Tutorial

Understanding the structure of a Java program is essential before writing any Java code. Java programs follow a strict structure to ensure readability, maintainability, and proper execution by the JVM.

1. Basic Structure of a Java Program

A typical Java program includes the following components:

  1. Package Declaration (Optional)
  2. Import Statements (Optional)
  3. Class Declaration (Required)
  4. Main Method (Required)
  5. Statements / Logic

Diagram of Structure


[Package Declaration] // optional
[Import Statements] // optional
public class ClassName { // class declaration
public static void main(String[] args) { // main method
// statements / logic
}
}

2. Package Declaration

  1. Packages are used to group related classes together.
  2. Declared at the top of the program.
  3. Syntax:

package com.example;

Example:


package mypackage;

class Demo {
public static void main(String[] args) {
System.out.println("Hello from package!");
}
}

3. Import Statements

  1. Used to import classes from other packages.
  2. Must be after package declaration and before class declaration.
  3. Syntax:

import java.util.Scanner;

Example:


import java.util.Scanner;

class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name:");
String name = sc.nextLine();
System.out.println("Hello " + name);
}
}

4. Class Declaration

  1. All Java code must reside inside a class.
  2. Syntax:

public class ClassName {
// class body
}

Rules:

  1. Class name should start with a capital letter (convention).
  2. File name must match the public class name.
  3. {} braces define the class body.

Example:


public class Car {
int speed;
String color;
}

5. Main Method

  1. The entry point of any Java program.
  2. Syntax:

public static void main(String[] args) {
// code to execute
}

Explanation of Each Keyword

KeywordMeaning
publicAccessible by JVM from anywhere
staticCan be called without creating object
voidReturns nothing
mainStandard method name
String[] argsAccepts command-line arguments

Example:


public class Hello {
public static void main(String[] args) {
System.out.println("Program started");
}
}

6. Statements / Logic

  1. The main method contains statements that define the program logic.
  2. Each statement ends with a semicolon ;.
  3. Java supports expression statements, method calls, and control structures.

Example:


int a = 10;
int b = 20;
int sum = a + b;
System.out.println("Sum is: " + sum);

7. Comments in Java

  1. Comments are used to document code.
  2. Types:
  3. Single-line comment: // Comment here
  4. Multi-line comment: /* Comment */
  5. Documentation comment: /** Comment */

Example:


// This is a single line comment
/* This is
a multi-line comment */

8. Complete Java Program Example


package mypackage; // optional

import java.util.Scanner; // optional

public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter first number:");
int num1 = sc.nextInt();

System.out.println("Enter second number:");
int num2 = sc.nextInt();

int sum = num1 + num2;
int diff = num1 - num2;

System.out.println("Sum: " + sum);
System.out.println("Difference: " + diff);
}
}

Explanation:

  1. Package groups class (optional)
  2. Import statement allows Scanner usage
  3. Class Calculator contains the main method
  4. Main method executes logic to calculate sum and difference

9. Rules for Java Program Structure

  1. Every Java program must have a class.
  2. File name must match the public class name.
  3. Main method is mandatory for standalone execution.
  4. Statements end with a semicolon.
  5. Java is case-sensitive.
  6. {} braces define blocks.
  7. Comments are optional but recommended.

10. Summary

  1. Java program starts with main method.
  2. Package and imports are optional but organize code.
  3. Class is mandatory and is the container for code.
  4. Statements inside main method define program behavior.
  5. Proper structure ensures readability, maintainability, and execution.