⏱️ 7 min read • Beginner Level • Lesson 6
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.
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.
A simple Java program usually follows this structure:
class ClassName {
public static void main(String[] args) {
// statements
}
}
main() method is the starting point of a Java application.
When you run a Java program, execution begins from main().
| 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. |
A Java program must have at least one class. A class is declared using the
class keyword.
class HelloWorld {
// class body
}
The main() method is the entry point of a Java application.
Java starts program execution from this method.
public static void main(String[] args) {
System.out.println("Hello Java");
}
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.
A statement is an instruction that tells Java to perform an action.
Most Java statements end with a semicolon ;.
System.out.println("Welcome to Java");
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.
class Example {
public static void main(String[] args) {
System.out.println("Inside main block");
}
}
In larger Java programs, package and import statements are often used at the top of the file.
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();
}
}
If a Java class is declared as public, the file name must match the public class name.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
HelloWorld.java because the public class name is HelloWorld.
When you run a Java program, Java follows these steps:
.java file..class file.main() method.HelloWorld.java → javac HelloWorld.java → HelloWorld.class → java HelloWorld
The following program shows a clean Java structure with class, main method, comment, and output statement.
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.
public class ProgramStructureExample defines the class.ProgramStructureExample.java.main() is the starting point of execution.System.out.println() prints output on the screen.; after a statement.Main instead of main. Java is case-sensitive.{ }.String[] args in the main method signature.main() method is the starting point of a Java application.🧠 Test your understanding with a quick quiz
Topic: Program-structure | Language: Java
🎉 Great job! Continue learning Java step by step.