Hello World — Class Declaration, main Method, and Compilation Process
In this tutorial, you will learn about Hello World. We cover key concepts, practical examples, and best practices to help you master this topic.
The Hello World program in Java demonstrates class declaration, the main method signature, compilation to bytecode, and execution on the JVM. While the program is only five lines long, every keyword — public, class, static, void, main, String[], System.out.println — has a specific purpose that reveals how Java works under the hood.
What You'll Learn
- The meaning of each keyword in a Java class declaration
- Why the
mainmethod signature is fixed - The compilation and class-loading process
- How
System.out.printlnworks
Why It Matters
Hello World is the smallest complete Java program. Understanding exactly what it does prepares you to debug compilation errors, understand public vs private access, and eventually design programs with multiple classes.
Real-World Use
Every Java developer writes and runs programs this way — whether in a local IDE, a CI pipeline, or an AWS Lambda. The same javac + java pattern underlies Maven, Gradle, and every build tool.
The Complete Program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Save this as Hello.java, then:
javac Hello.java
java Hello
Expected output:
Hello, World!
Dissecting the Keywords
public class Hello
public— an access modifier that makes this class accessible from anywhere. If you omitpublic, the class has package-private visibility and is only accessible within its package.class— a keyword that declares a class. Java is class-based; every piece of code lives inside a class.Hello— the class name. By convention, class names are PascalCase. The file must be namedHello.javabecause Java requires the public class name to match the file name.
A class can have fields (variables), methods, constructors, and nested types. In this minimal program, the class contains only one method.
public static void main(String[] args)
This is the entry point of every standalone Java application. The JVM calls this method when you run java Hello.
public— the JVM needs unrestricted access to call this method. If it were private, the JVM could not invoke it.static— the JVM callsmainbefore any objects exist. A static method belongs to the class itself, not to instances.void— the method returns nothing. Java programs exit by callingSystem.exit()or by reaching the end ofmain.main— the method name the JVM looks for. Must be spelled exactlymain.String[] args— command-line arguments as an array of strings. Even if unused, this parameter is required. You can writeString[] argsorString... args(varargs).
System.out.println("Hello, World!");
System— a built-in Java class in thejava.langpackage, automatically imported.System.out— a static field inSystemof typePrintStream, initialized by the JVM to the standard output stream (the terminal).println— a method onPrintStreamthat prints the argument followed by a newline."Hello, World!"— a string literal enclosed in double quotes.
The Compilation Process
Source Code (.java) -> Lexical Analysis -> Parsing -> Semantic Analysis -> Bytecode Generation -> .class file
- Lexical Analysis: The compiler breaks the source into tokens — keywords (
public,class), identifiers (Hello,main), separators ({,}), operators, and literals. - Parsing: The tokens are arranged into an Abstract Syntax Tree (AST) that represents the program's structure according to the Java grammar.
- Semantic Analysis: The compiler checks types, resolves symbols, and verifies that code follows Java's rules (e.g.,
voidmethods cannot return a value). - Bytecode Generation: The compiler produces
.classfiles containing bytecode — platform-independent instructions for the JVM.
What Bytecode Looks Like
Use the javap tool to disassemble the bytecode:
javap -c Hello
Expected output:
Compiled from "Hello.java"
public class Hello {
public Hello();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #13 // String Hello, World!
5: invokevirtual #15 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}
Each line is a JVM instruction. For example, getstatic #7 loads the System.out field, ldc #13 loads the string constant from the constant pool, and invokevirtual #15 calls the println method.
Class Loading and Execution
When you run java Hello:
- The Bootstrap class loader loads core JRE classes from
rt.jar(orlib/modulesin Java 9+). - The system class loader loads
Hello.classfrom the current directory (or classpath). - The JVM verifies the bytecode for correctness and security violations.
- The JVM calls the
mainmethod. Execution begins. - When
mainfinishes, the JVM exits (unless there are non-daemon threads still running).
Common Mistakes
- Forgetting the
String[] argsparameter. The JVM will not recognizemain()without it. You will getMain method not found in class Hello. - Using
stringinstead ofString. Java is case-sensitive.Stringis the correct class;stringwill not compile. - Writing
maininstead ofmain. A typo likemiancompiles (as a regular method) but the JVM will not find the entry point. - Naming the file differently from the class.
class Hellomust be inHello.java, or you get a compilation error. - Running
java Hello.classinstead ofjava Hello. The JVM expects a class name, not a file name. Append.classcauses a load error.
Practice Questions
1. Why must the main method be public static void?
public so the JVM can access it, static so it can be called without an instance, and void because the program exits via System.exit() rather than a return value.
2. What is the difference between println and print?
println appends a newline after the output; print does not. So System.out.print("Hello"); System.out.print(" World") prints Hello World on one line.
3. What does the javap tool do?
It disassembles compiled .class files, showing the bytecode instructions, the constant pool, and method signatures.
4. What happens if you declare main as private?
The code compiles, but the JVM reports Main method not found in class Hello because it looks for a public method.
5. What is the purpose of the String[] args parameter?
It receives command-line arguments. For example, java Hello Alice Bob passes an array with ["Alice", "Bob"].
Challenge Question:
Write a Java program that prints all command-line arguments. Compile and run it with java ArgsDemo one two three. The output should list each argument on its own line. Use a for-each loop.
FAQ
Mini Project
Create a program AboutMe.java that:
- Declares a class
AboutMewith amainmethod - Uses
System.out.printlnto print your name, age, and favorite programming language on separate lines - Uses
System.out.printwith\nto produce the same output - Compiles both with and without the
-gflag (debug info), and compare the sizes of the resulting.classfiles usingls -lordir - Run
javap -c -pon the compiled class to see the default constructor and the main method bytecode
Reflect on why the compiler added a default constructor even though you did not write one.
What's Next
Now you understand the basic structure of a Java program. The next lesson introduces variables and data types — the eight primitive types, type conversion, type inference with var, and how default values work. This is where you start building programs that store and manipulate data.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro