Java Program Structure - Class, main Method and Syntax

⏱️ 7 min read • Beginner Level • Lesson 6

Lesson 6 of 124 of Java Tutorial

A Java program structure defines how code is organized inside a Java file. A basic Java program usually contains a class, a main method, statements, braces, and sometimes package/import statements.

Before starting this lesson, make sure you have completed Java environment setup. If you are new to Java, you can also read Java introduction.


What is Java Program Structure?

Java program structure means the standard arrangement of code in a Java program. It tells where to write the class, where to write the main() method, where statements are placed, and how blocks are opened and closed using braces.

Simple Meaning: Java program structure is like the layout of a house. A Java file has different sections, and each section has a specific purpose.

Basic Java Program Structure

A simple Java program usually follows this structure:

BasicStructure.java
Copy Download
class ClassName {
    public static void main(String[] args) {
        // statements
    }
}
Important: The main() method is the starting point of a Java application. When you run a Java program, execution begins from main().

Parts of a Java Program

Part Purpose
class Defines a class in Java.
main() method Starting point of program execution.
Statements Instructions executed by Java.
Braces { } Define blocks of code.
Semicolon ; Ends a statement.
package Groups related classes together.
import Allows using classes from other packages.

Class Declaration in Java

A Java program must have at least one class. A class is declared using the class keyword.

ClassDeclaration.java
Copy Download
class HelloWorld {
    // class body
}
Note: Class name should usually start with a capital letter according to Java naming conventions.

main() Method in Java

The main() method is the entry point of a Java application. Java starts program execution from this method.

MainMethod.java
Copy Download
public static void main(String[] args) {
    System.out.println("Hello Java");
}

Explanation

  • public allows JVM to access the method.
  • static allows JVM to call the method without creating an object.
  • void means the method does not return any value.
  • main is the method name.
  • String[] args stores command-line arguments.

Statements and Semicolon

A statement is an instruction that tells Java to perform an action. Most Java statements end with a semicolon ;.

StatementExample.java
System.out.println("Welcome to Java");

Braces and Code Blocks

Curly braces { } are used to define code blocks in Java. A class has a block, a method has a block, and control statements also use blocks.

BlockExample.java
class Example {
    public static void main(String[] args) {
        System.out.println("Inside main block");
    }
}

Package and Import Statements

In larger Java programs, package and import statements are often used at the top of the file.

PackageImportExample.java
package myapp;

import java.util.Scanner;

public class Example {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your name:");
        String name = input.nextLine();
        System.out.println("Hello " + name);
        input.close();
    }
}
Important: If a file has a package declaration, it must be the first statement in the Java source file.

Java File Name Rule

If a Java class is declared as public, the file name must match the public class name.

HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}
Correct: File name should be HelloWorld.java because the public class name is HelloWorld.

Execution Flow of a Java Program

When you run a Java program, Java follows these steps:

  1. Program is written in a .java file.
  2. Java compiler compiles the source code into bytecode.
  3. Bytecode is stored in a .class file.
  4. JVM loads the class.
  5. JVM starts execution from the main() method.
Execution Flow
HelloWorld.java → javac HelloWorld.java → HelloWorld.class → java HelloWorld

Complete Java Program Structure Example

The following program shows a clean Java structure with class, main method, comment, and output statement.

ProgramStructureExample.java
Copy Try Download
public class ProgramStructureExample {
    public static void main(String[] args) {
        // This statement prints a message
        System.out.println("Java program structure is easy to understand.");
    }
}

Output:

Java program structure is easy to understand.

Explanation

  1. public class ProgramStructureExample defines the class.
  2. The file name should be ProgramStructureExample.java.
  3. main() is the starting point of execution.
  4. The comment explains the next line of code.
  5. System.out.println() prints output on the screen.

Common Mistakes in Java Program Structure

  • Forgetting the semicolon ; after a statement.
  • Writing class name and file name differently when the class is public.
  • Writing Main instead of main. Java is case-sensitive.
  • Missing opening or closing braces { }.
  • Writing code outside the class body.
  • Forgetting String[] args in the main method signature.
Summary:
  • A Java program is usually written inside a class.
  • The main() method is the starting point of a Java application.
  • Statements usually end with a semicolon.
  • Braces define code blocks.
  • If a class is public, the file name must match the class name.
  • Package and import statements are written at the top of the file.

Next step: Write and run your first Java program.

🚀 Continue to First Java Program →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Program-structure | Language: Java

⚠️ No quiz found for program-structure

🎉 Great job! Continue learning Java step by step.