Java

Structure of a Simple Java Program and Understanding `public static void main`

By Utility Zone · 2025-11-06T09:31:45.029345

A Java program follows a well-defined structure that organizes code into logical components. Understanding this structure is fundamental for writing effective Java applications, and the main method serves as the critical entry point where execution begins.

Overall Structure of a Java Program

Hierarchical structure of a Java program showing how different components are organized

Hierarchical structure of a Java program showing how different components are organized

A complete Java program typically consists of the following components, organized from top to bottom:12

Documentation Section (Optional)

This section, written in Javadoc format using /** */ comments, provides information about the program's purpose, version, author, and usage instructions. While optional, documentation improves code maintainability and helps other developers understand the program's functionality.2

Package Statement (Optional)

The package statement declares which package the class belongs to, enabling code organization into logical groups. This is particularly useful for large projects with multiple classes. For example:12

package com.mycompany.utilities;

Import Statements (Optional)

Import statements allow the program to use pre-defined classes from other packages. By default, Java automatically imports the java.lang.* package. Additional imports might look like:21

import java.util.Scanner;
import java.util.ArrayList;

Class Definition

Every Java program requires at least one class. The class is declared using the class keyword, and by convention, the filename must match the public class name:31

public class HelloWorld {
    // Class body
}

Class Body Components

Inside a class, you define:41

  • Data members (variables): Store state information for objects created from the class
  • Constructors: Special methods that initialize new objects automatically
  • User-defined methods: Implement specific functionality and logic
  • main() method: The entry point for program execution

A Simple Java Program Example

Here's a complete example of a basic Java program:35

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

Let's break down each component:

  • public: Access modifier making the class accessible from anywhere
  • class: Keyword declaring a class named HelloWorld
  • main() method: Where the program starts executing
  • System.out.println(): Built-in method that prints text to the console

Understanding public static void main(String[] args)

This method signature is mandatory and strictly defined. Let's examine each keyword:675

1. public - Access Modifier

The public keyword makes the main method accessible to the Java Virtual Machine (JVM) from outside the class.789

Why it's necessary: The JVM needs to call the main method from outside the program to launch the application. If main were private or protected, the JVM wouldn't have access to invoke it, resulting in a runtime error.897

What happens without it: If main is declared as private, the program will compile successfully but fail at runtime with a "Main method not found" error.9

2. static - Non-Instance Method Marker

The static keyword designates main as a class-level method rather than an instance method.678

What this means: Static methods belong to the class itself, not to individual objects created from the class. They can be called without creating an instance (object) of the class.86

Why it's necessary: The JVM needs to call main before any objects are instantiated. If main were not static, the JVM would need to create an object of the class first, which would be wasteful and unnecessary.68

Example of the difference:

// Static method - called on the class
ClassName.staticMethod();

// Instance method - requires an object
ClassName obj = new ClassName();
obj.instanceMethod();

3. void - Return Type

The void keyword indicates that the main method returns no value.101112

What this means: Unlike methods that return data (e.g., int add() or String getName()), void methods execute statements but don't send back any result. They perform actions such as printing output or modifying state, but don't compute a value to return to the caller.1112

Comparison:

// Void method - performs action, returns nothing
public static void printMessage() {
    System.out.println("Hello");
}

// Non-void method - returns an integer value
public static int add(int a, int b) {
    return a + b;  // Must include return statement
}

4. String[] args - Command-Line Arguments

The parameter String[] args is an array of strings that captures command-line arguments passed to the program at runtime.13147

How it works: When you run a Java program from the command line, you can pass arguments that are automatically stored in this array:1413

java HelloWorld argument1 argument2 argument3

In this example, the args array contains:

  • args = "argument1"
  • args[^20] = "argument2"
  • args[^21] = "argument3"

Practical example:

public class CommandLineDemo {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("No arguments provided");
        } else {
            for (int i = 0; i < args.length; i++) {
                System.out.println("Argument " + i + ": " + args[i]);
            }
        }
    }
}

Complete Program Structure Example

Here's a more comprehensive example demonstrating all components:13

// Documentation Section
/**
 * This program calculates the area of a rectangle.
 * Author: Developer Name
 * Date: 2025
 */

// Package Statement
package com.geometry.shapes;

// Import Statements
import java.util.Scanner;

// Class Definition
public class Rectangle {
    
    // Data Members (Variables)
    private double length;
    private double width;
    
    // Constructor
    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }
    
    // User-defined Method
    public double calculateArea() {
        return length * width;
    }
    
    // main() Method - Entry Point
    public static void main(String[] args) {
        Rectangle rect = new Rectangle(5.0, 3.0);
        double area = rect.calculateArea();
        System.out.println("Area of rectangle: " + area);
    }
}

Output: Area of rectangle: 15.0


Key Points to Remember

The main method must follow this exact signature:15

  • Must be public to be accessible to the JVM
  • Must be static so it can be called without creating an object
  • Must return void (no return value)
  • Must accept String[] args as a parameter
  • Must be defined inside a class (Java is object-oriented)

Without any of these requirements, your Java program will either fail to compile or produce a runtime error. This rigid specification ensures that the JVM has a standard, predictable way to start any Java application, making Java's execution model consistent across all platforms.51 <span style="display:none">1516171819</span>


<div align="center">⁂</div>

Footnotes

  1. https://www.geeksforgeeks.org/java/structure-and-members-of-the-java-program/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8

  2. https://ethans.co.in/blogs/what-is-the-structure-of-a-java-program/ ↩ ↩2 ↩3 ↩4

  3. https://www.edureka.co/blog/structure-of-a-java-program/ ↩ ↩2 ↩3

  4. https://syntaxdb.com/ref/java/class-declare ↩

  5. https://www.tutorialspoint.com/java-public-static-void-main-string-args ↩ ↩2 ↩3 ↩4

  6. https://www.geeksforgeeks.org/java/understanding-static-in-public-static-void-main-in-java/ ↩ ↩2 ↩3 ↩4

  7. https://www.digitalocean.com/community/tutorials/public-static-void-main-string-args-java-main-method ↩ ↩2 ↩3 ↩4 ↩5

  8. https://www.geeksforgeeks.org/java/java-main-method-public-static-void-main-string-args/ ↩ ↩2 ↩3 ↩4 ↩5

  9. https://unstop.com/blog/main-method-in-java ↩ ↩2 ↩3

  10. https://www.scaler.com/topics/java/structure-of-java-program/ ↩

  11. https://www.w3schools.com/java/java_methods_return.asp ↩ ↩2

  12. https://runestone.academy/ns/books/published/csjava/Unit5-Writing-Methods/topic-5-3-method-return.html ↩ ↩2

  13. https://stackoverflow.com/questions/890966/what-is-the-string-args-parameter-in-the-main-method ↩ ↩2

  14. https://www.scaler.com/topics/string-args-in-java/ ↩ ↩2

  15. https://www.tutorialspoint.com/java/java_class_methods.htm ↩

  16. https://jcoder.io/content/course/java/beginners/ch05/class-methods-fields-declaration ↩

  17. https://www.reddit.com/r/learnjava/comments/1582fsd/im_trying_to_understand_the_difference_between/ ↩

  18. https://www.geekster.in/articles/command-line-arguments-java/ ↩

  19. https://www.w3schools.com/java/ref_keyword_public.asp ↩