Java Program Structure
Every Program Lives in a Class
In Java, all code belongs to a class. There are no standalone functions, no global variables, and no code outside of a class definition. Even the simplest program requires wrapping logic inside a class and a method.
This is a defining characteristic of Java’s object-oriented design: the class is the fundamental unit of organisation.
The main Method
The Java Virtual Machine (JVM) looks for a specific method to start execution. The entry point must have exactly this signature:
public static void main(String[] args)
Each keyword serves a specific purpose:
| Keyword | Meaning |
|---|---|
public | Accessible to the JVM from outside the class. If it were private, the JVM could not find it. |
static | Belongs to the class, not to an instance. No object of the class exists yet when the program starts, so the JVM must be able to call the method without creating one. |
void | Returns nothing. The JVM does not expect a return value; the program terminates when main returns or when System.exit() is called. |
String[] args | An array of command-line arguments. The JVM populates this from whatever follows the class name on the command line. |
The name main is conventional and mandatory — the JVM looks specifically for main, not start or run or entryPoint.
Command-Line Arguments
java MyProgram hello 42 "some text"
Inside the program, args will be:
args[0] = "hello"
args[1] = "42"
args[2] = "some text"
All arguments arrive as String objects regardless of type. If you need numbers, you must parse them:
int n = Integer.parseInt(args[0]);
Console Output
System.out.println() sends text to standard output, followed by a newline. System.out.print() does the same without a newline.
System.out.println("Hello, World!");
System.out.print("No newline after this.");
System.out is a PrintStream object — a static field of the System class. println is overloaded for all primitive types and for objects (calling the object’s toString() method).
Annotated HelloWorld
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
if (args.length > 0) {
System.out.println("First argument: " + args[0]);
}
}
}
To compile and run:
javac HelloWorld.java
java HelloWorld
java HelloWorld Alice
Package Declarations
A package is a namespace that organises classes and prevents name collisions. The package declaration must be the first non-comment line in the file:
package com.example.myapp;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello from a package!");
}
}
Package names follow a reversed-domain convention (com.example.myapp), and the directory structure mirrors the package hierarchy:
src/
com/
example/
myapp/
HelloWorld.java
If no package is declared, the class belongs to the unnamed default package — fine for small programs, but real projects always use packages.
Import Statements
The import statement lets you refer to classes from other packages by their short name rather than their fully qualified name:
import java.util.ArrayList;
import java.util.List;
Without the import, you would write java.util.ArrayList every time. A wildcard import (import java.util.*) imports all classes from the package but not subpackages.
java.lang is imported automatically — classes like String, Math, Integer, and System are always available without an explicit import.
Java Syntax Rules
- Case sensitivity:
count,Count, andCOUNTare three distinct identifiers - Semicolons: every statement ends with a semicolon. Forgetting one is a compile error.
- Braces: code blocks are delimited by
{and}. Indentation is for human readers only — the compiler ignores whitespace. - One public class per file: a
.javafile may contain multiple classes, but at most one of them can be public. The public class’s name must match the filename.
Class Naming Conventions
- Use PascalCase (also called UpperCamelCase):
HelloWorld,BankAccount,ArrayList - Class names should be nouns or noun phrases
- The filename must match the public class name exactly, including capitalisation:
HelloWorld.javacontainspublic class HelloWorld
Summary of File Layout
package com.example; /* package declaration (optional) */
import java.util.ArrayList; /* import statements (optional) */
import java.util.List;
public class ClassName { /* class definition — must match filename */
private int field; /* fields (state) */
public ClassName() { /* constructors */
this.field = 0;
}
public int getField() { /* methods (behaviour) */
return this.field;
}
public static void main(String[] args) { /* entry point */
ClassName obj = new ClassName();
System.out.println(obj.getField());
}
}