Skip to main content

ProwessApps

Learn · Practice · Excel

Java Basics

Java Program Structure

A Java program structure defines how code is organized inside a source file. A basic program normally contains a class, a main() method, statements, and braces, while larger programs may also begin with package and import declarations.

11 min read Beginner Lesson 6 of 124
Lesson 6 of 124 in the Java roadmap

Quick answer

What is the structure of a Java program?

A Java source file can contain a package declaration, import declarations, and one or more classes. Program execution normally begins in a class method declared as public static void main(String[] args). Statements are placed inside code blocks enclosed by braces, and most statements end with a semicolon.

What you will learn

  • How a basic Java source file is organized.
  • The purpose of classes, main(), statements, semicolons, and braces.
  • Where package and import declarations belong.
  • How the public class name controls the source filename.
  • How source code moves from a .java file to JVM execution.

Before you begin

Complete Java Environment Setup so that the java and javac commands are available. If Java is new to you, first review Introduction to Java.

What is Java program structure?

Java program structure is the standard arrangement of declarations and statements in a Java source file. It shows where a class is declared, where methods are written, where executable statements belong, and how braces define the boundaries of each code block.

A simple way to understand it

Think of a Java source file as a building plan. Package and import declarations identify the location and external resources, a class forms the main structure, methods define rooms for behavior, and statements describe the work performed inside those rooms.

Basic Java program structure

A simple executable Java program normally follows this structure:

BasicStructure.java
class ClassName {
    public static void main(String[] args) {
        // statements
    }
}

Where execution begins

For this type of Java application, the launcher starts execution from the main() method. A source file can also define classes that do not contain main(); those classes are used by other parts of an application.

Parts of a Java program

PartPurpose
classDeclares a class that can contain fields, constructors, and methods.
main() methodProvides the conventional entry point for a launched Java application.
StatementsExpress instructions and actions performed by the program.
Braces { }Define class, method, and control-flow blocks.
Semicolon ;Terminates many Java statements.
packageDeclares the package to which a source file belongs.
importAllows types from other packages to be referenced by simple names.

Class declaration in Java

Java code is organized into classes. A class declaration uses the class keyword, followed by the class name and a body enclosed in braces.

ClassDeclaration.java
class HelloWorld {
    // class body
}

By Java naming convention, class names normally use PascalCase and begin with an uppercase letter, such as HelloWorld or StudentRecord.

The main() method in Java

The following declaration is the conventional entry point used by the Java launcher:

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

Meaning of each part

  • public: makes the method accessible to the Java launcher.
  • static: allows the method to be invoked without first creating an object.
  • void: indicates that the method does not return a value.
  • main: is the method name recognized as the application entry point.
  • String[] args: receives command-line arguments as an array of strings.
ADVERTISEMENT

Statements and semicolons

A statement is an instruction that tells Java to perform an action. Many statements end with a semicolon, which marks the end of the instruction.

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

A semicolon does not follow every construct. For example, a class declaration, method declaration, or if block ends with a closing brace rather than a semicolon.

Braces and code blocks

Curly braces { } group statements into blocks. Classes, methods, loops, and conditional statements use blocks to define where their contents begin and end.

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

Package and import declarations

Larger applications organize related classes into packages. Import declarations allow a source file to use types from other packages without writing their fully qualified names repeatedly.

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();
    }
}

Declaration order

When present, the package declaration appears before import declarations. Class and interface declarations follow the imports. Comments and whitespace may appear around them.

Java filename rule

When a top-level class is declared public, its source filename must match the class name exactly, including uppercase and lowercase letters, followed by .java.

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

The correct filename is HelloWorld.java because the public class is named HelloWorld.

Execution flow of a Java program

  1. Write the program in a .java source file.
  2. Compile the source code with javac.
  3. The compiler creates bytecode in a .class file.
  4. The java launcher starts a JVM and loads the class.
  5. The launcher invokes the class's main() method.

Complete Java program structure example

The following example combines a public class, the main method, a comment, and a statement.

ProgramStructureExample.java
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.

How the example works

  • public class ProgramStructureExample declares the public class.
  • The source file must be named ProgramStructureExample.java.
  • main() is the entry point used to start the program.
  • The comment describes the purpose of the following statement.
  • System.out.println() prints the message followed by a new line.

Common mistakes in Java program structure

  • Forgetting the semicolon after a statement.
  • Using a filename that does not match a public class name.
  • Writing Main instead of main; Java is case-sensitive.
  • Missing an opening or closing brace.
  • Writing executable statements directly outside a class or method body.
  • Using an incorrect main() signature when expecting the Java launcher to find it.
Incorrect filename

Program.java containing public class HelloWorld

Correct filename

HelloWorld.java containing public class HelloWorld

What’s next?

Continue with First Java Program to create, compile, and run a complete Java source file yourself.

Lesson summary

  • A Java source file organizes declarations and executable code into a defined structure.
  • Classes contain fields, constructors, methods, and nested declarations.
  • The conventional application entry point is public static void main(String[] args).
  • Most statements end with semicolons, while braces define code blocks.
  • Package declarations precede imports, and imports precede type declarations.
  • A public top-level class must have the same name as its .java file.

Frequently asked questions

What is the basic structure of a Java program?

A basic executable Java program contains a class and a public static void main(String[] args) method. Executable statements are written inside the main method or other methods, and braces define each code block.

Why is the main method important in Java?

The Java launcher uses the main method as the conventional entry point for a launched application. Execution begins with public static void main(String[] args).

Must every Java class contain a main method?

No. Only a class used as an application entry point needs a suitable main method. Other classes can provide data and behavior used by the application.

Why must a public class name match the Java filename?

A public top-level class must be stored in a source file with the same case-sensitive name followed by the .java extension. For example, public class HelloWorld belongs in HelloWorld.java.

Where are package and import declarations written?

When present, the package declaration appears before import declarations. Imports appear before class, interface, enum, or record declarations.

Do all Java statements end with a semicolon?

Many Java statements end with a semicolon, but declarations and control-flow blocks that end with a closing brace do not normally require a semicolon.

Check your knowledge

ADVERTISEMENT

Free learner features

Save your learning progress

Sign in to mark lessons as completed, save your Java learning progress across devices, and participate in lesson discussions.

Sign in to track progress

Discussion

Ask questions, share suggestions, or discuss this lesson.


No approved comments yet. Start the discussion by asking a helpful question.

Java Tutorial for Beginners

Choose a lesson