Index

Java Program Structure and Entry Point

Java Syntax

1.1 Structure of a Java Program

Understanding the structure of a Java program is the foundation of learning the language. Java follows a strict syntax and organizational style that ensures consistency and clarity, making code easier to read, maintain, and compile. Every Java program is composed of classes and methods, with a designated entry point—the main method—where execution begins.

Let's start with a minimal Java program:

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

This small example includes all the essential building blocks of a Java application. Let's break it down:

public class HelloWorld

Every Java application must have at least one class. In Java, everything resides within a class, even the entry point to the program. Here, HelloWorld is the name of the class. The keyword public makes it accessible from outside the class, including the Java Virtual Machine (JVM), which needs to invoke it.

public static void main(String[] args)

This is the entry point of any standalone Java application. The JVM looks for this exact method signature to start the program.

Without this method, the program will compile but not run.

Curly Braces { }

Java uses curly braces to define blocks of code:

Each opening brace must be matched by a closing brace. This block structure defines scope, and misplacing or omitting braces results in a compile-time error.

System.out.println("Hello, Java!");

This is a statement that prints a line of text to the console. It uses the System.out output stream and calls its println() method to display the message.

Index

1.2 The main Method Syntax

The main Method Syntax

The main method is the entry point of any standalone Java application. When you run a Java program, the Java Virtual Machine (JVM) looks specifically for this method to start executing your code. Because of this, its syntax must be exact.

Here is the correct syntax for the main method:

public static void main(String[] args) {
    // program statements go here
}

Let's break down each part of this declaration:

Common Syntax Error: Missing static

A very common mistake is to forget the static keyword:

public void main(String[] args) { // Incorrect!
    // ...
}

This compiles but will result in a runtime error: Error: Main method not found in class ...

Because without static, the JVM would need to create an instance of the class before calling main, but it cannot do this on its own.

Reflection

The main method is special because it signals where the Java program begins. Its exact signature is required by the JVM to correctly locate and invoke it. This design enforces consistency, allowing the JVM to start any Java program in a predictable way. As you continue learning Java, remember that while you can create many other methods with different names and signatures, the main method is your program's official starting point.

Index

1.3 Class Declaration and File Structure

Class Declaration and File Structure

In Java, classes are the fundamental building blocks of programs. Understanding how to declare classes and how they relate to your source files is crucial for writing well-structured code.

Declaring a Java Class

A class declaration typically looks like this:

public class MyClass {
    // class body
}

Filename Must Match the Public Class

When a class is declared public, the name of the file must exactly match the class name and end with .java. For example, if your class is:

public class MyClass { }

then the filename must be MyClass.java. This rule is enforced by the Java compiler and is crucial for correct compilation and program organization.

Valid and Invalid Naming Examples

Class Declaration Filename Valid?
public class HelloWorld HelloWorld.java ✅ Valid
class HelloWorld HelloWorld.java ✅ Valid (no public)
public class HelloWorld helloWorld.java ❌ Invalid (case mismatch)
public class HelloWorld Hello.java ❌ Invalid (name mismatch)

Multiple Classes in One File

Java allows multiple classes in a single .java file, but only one of them may be declared public, and if so, the file name must match that public class.

For example, this is valid in a file named MainApp.java:

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

class Helper {
    void assist() {
        System.out.println("Helping...");
    }
}
Index

1.4 Statements, Blocks, and Semicolons

Statements, Blocks, and Semicolons

In Java, a statement is a complete unit of execution—something the program does, such as declaring a variable or calling a method. Most statements end with a semicolon (;), which tells the compiler where one statement finishes and the next begins.

Examples of Statements

int number = 10;           // variable declaration statement
number = number + 5;       // assignment statement
System.out.println(number); // method call statement

Each of these lines is a statement and must end with a semicolon. Missing a semicolon causes a compile-time error because the compiler cannot determine where the statement ends.

Blocks: Grouping Statements

A block is a group of statements enclosed in curly braces {}. Blocks are used to define the body of methods, classes, and control structures like loops and conditionals.

Example:

if (number > 10) {
    System.out.println("Number is greater than 10");
    number = 10;
}

Here, the two statements inside the {} form a block that executes only if the condition is true.

Blocks can be nested and help organize code logically.

Semicolon Placement

Incorrect example:

if (number > 10); {  // Incorrect semicolon here!
    System.out.println("This always prints");
}

Reflection

Java does not enforce indentation or whitespace, but proper formatting is crucial for human readers. Using consistent indentation and clear block organization makes code easier to understand and maintain. Blocks clearly indicate the scope of statements and control flow. While semicolons are small punctuation marks, they are fundamental to Java's syntax; missing or misplaced semicolons are a frequent cause of compilation errors for beginners. Paying attention to statements, blocks, and semicolons early will build a strong foundation for writing syntactically correct Java programs.

Index

1.5 Command-Line Arguments Syntax

Java programs can accept input parameters when started from the command line. These inputs, called command-line arguments, are passed to the main method as an array of strings: String[] args.

Using String[] args

The parameter args in the main method holds all command-line arguments as strings. You can access them by their index, starting at args[0].

Here is an example program that prints all command-line arguments:

public class PrintArgs {
    public static void main(String[] args) {
        System.out.println("Number of arguments: " + args.length);
        for (int i = 0; i < args.length; i++) {
            System.out.println("Argument " + i + ": " + args[i]);
        }
    }
}

Running the Program with Arguments

To run this program from the terminal:

  1. Compile it:
javac PrintArgs.java
  1. Run it with arguments, for example:
java PrintArgs hello world 123

Output:

Number of arguments: 3
Argument 0: hello
Argument 1: world
Argument 2: 123

Each word after the class name is treated as a separate argument and passed to the args array.

Index

1.6 Comments and Formatting Best Practices

Comments are lines in your code that the Java compiler ignores. They help programmers explain what the code does, making it easier to read and maintain.

Types of Comments

// This is a single-line comment
int number = 10; // Inline comment after a statement
/*
 This is a multi-line comment.
 It can cover several lines.
*/
/**
 * This method prints a greeting message.
 * @param name the name to greet
 */
public void greet(String name) {
    System.out.println("Hello, " + name);
}

JavaDoc comments help create API documentation and should be used on classes, methods, and important fields.

When and Why to Comment

Comments should explain why something is done, clarify complex logic, or provide context—not just restate what the code clearly shows. Over-commenting trivial code or outdated comments can confuse readers. Good comments improve collaboration and ease future maintenance.

Sample with Good Formatting and Comments

public class Example {

    public static void main(String[] args) {
        // Print a welcome message
        System.out.println("Welcome to Java Syntax!");

        /*
         * Initialize a counter and increment it.
         * This simulates a simple loop without actual looping.
         */
        int counter = 0;
        counter++;
        System.out.println("Counter value: " + counter);
    }
}

Reflection

Consistent indentation and clean formatting are essential for readability, even though Java ignores whitespace. Use comments thoughtfully to guide readers without cluttering code. Clear structure and meaningful comments make your code professional and easier to understand by others — or yourself in the future.

Index