Java from scratch syntax class main public static void println guide FP2

Java from scratch — class structure, main and basic syntax really explained

Java from scratch means understanding why every line of the skeleton program looks the way it does — not just copying it blindly. Every FP2 student starts with the same question: why does a program that just prints “Hello” need five lines and three keywords they’ve never seen before? This article answers that, then covers every element of Java’s basic syntax with direct comparisons to Python so nothing feels arbitrary.

The minimal Java program — every line explained

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

This is the smallest valid Java program. Let’s take it apart line by line.

<strong>public class HelloWorld</strong>

In Java every piece of code lives inside a class. There are no standalone functions — unlike Python where you can write def my_function(): at the top level of a file. Everything belongs to a class.

public — an access modifier. It means this class is visible from anywhere. For now treat it as a required keyword for your main class.

class — the keyword that declares a class.

HelloWorld — the name of the class. This must match the filename exactly, including capitalisation. If the file is HelloWorld.java, the class must be HelloWorld. If they don’t match, Java refuses to compile with the error “class HelloWorld is public, should be declared in a file named HelloWorld.java”.

{ — opens the class body. Everything between this { and the matching } belongs to the class.

<strong>public static void main(String[] args)</strong>

This is the entry point — Java starts execution here. This exact signature is mandatory. Change any word and Java can’t find where to start:

public — the method is accessible from anywhere. Required for main — the JVM needs to call it from outside your class.

static — the method belongs to the class itself, not to a specific instance (object) of the class. The JVM calls main before creating any objects, so it must be static. In FP2 you’ll write most methods as instance methods (without static), but main always has it.

void — this method returns nothing. Compare with functions in Python that return None by default — void is the explicit equivalent.

main — the special name the JVM looks for. Not Main, not start, not anything else — exactly main.

String[] args — an array of strings containing any command-line arguments passed when running the program. You won’t use args in FP2, but the parameter must be there. String[] means “array of String” — the [] after the type denotes an array.

System.out.println("Hello, World!")

The Java equivalent of Python’s print().

System — a built-in class in Java’s standard library that provides access to system resources.

out — a static field of System that represents the standard output stream (the terminal).

println — short for “print line”. Prints the argument followed by a newline. print (without ln) prints without a newline at the end.

; — the semicolon at the end. Every statement in Java ends with a semicolon. This is the single most common syntax error for Python programmers — forgetting the semicolon. In Python the newline ends the statement. In Java the semicolon does.

} — closes the method body, then the class body.

Python vs Java — the direct comparison

# Python — 1 line
print("Hello, World!")
// Java — 5 lines minimum
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Java is more verbose — that’s intentional. Java was designed for large software projects where structure and explicitness prevent errors. Python was designed for simplicity and rapid development. Neither is better — they’re optimised for different things. In FP2 you learn Java because it teaches object-oriented programming in its most explicit form.

Comments in Java

Java has three types of comments:

// Single-line comment — from // to end of line

/* Multi-line comment
   can span several lines
   used for longer explanations */

/**
 * Javadoc comment — for documentation
 * Used to document classes and methods
 * @param args command-line arguments
 * @return nothing (void)
 */
public static void main(String[] args) {

In FP2 you’ll mainly use // for single-line comments. Javadoc (/** */) is used in professional code to generate documentation automatically — you’ll see it in FP2 exercises.

Output — System.out.println and friends

System.out.println("With newline at end");     // moves to next line
System.out.print("Without newline");            // stays on same line
System.out.printf("Formatted: %d %.2f %s\n",   // C-style formatting
                  42, 3.14, "hello");
# Python equivalents
print("With newline")                # println
print("Without newline", end="")     # print without \n
print(f"Formatted: {42} {3.14:.2f} {'hello'}")  # f-string

printf in Java works exactly like C’s printf — the same format specifiers (%d, %f, %s, %c) and the same \n for newlines. This is useful for formatted tables and aligned output.

Escape sequences

System.out.println("Newline:\nSecond line");
System.out.println("Tab:\there");
System.out.println("Quote: \"hello\"");
System.out.println("Backslash: \\");

Output:

Newline:
Second line
Tab:	here
Quote: "hello"
Backslash: \

The escape sequences are identical to Python and C.

Curly braces and indentation

In Python, indentation defines code blocks — it’s enforced by the language. In Java, { } curly braces define blocks. Indentation is for human readability only — the compiler ignores it completely.

# Python — indentation is structural
if x > 5:
    print("big")
    print("still big")
print("always")
// Java — { } are structural, indentation is decorative
if (x > 5) {
    System.out.println("big");
    System.out.println("still big");
}
System.out.println("always");

This means valid Java can be written with terrible indentation:

// Compiles and runs — but terrible style
public class Bad{public static void main(String[] args){System.out.println("works");}}

VS Code’s auto-formatter (Shift + Alt + F) fixes indentation automatically. Always use it.

The case sensitivity rule

Java is completely case-sensitive:

String name = "Sergio";    // correct
string name = "Sergio";    // compile error — String not string
System.out.println(name);  // correct
system.out.println(name);  // compile error — System not system
public class HelloWorld {}  // correct
public class helloworld {}  // compile error — filename must match exactly

This catches many beginners — String (capital S) is a class, string (lowercase) doesn’t exist in Java.

Your first multi-line program

public class Profile {
    public static void main(String[] args) {
        // Student information
        String name = "Sergio";
        int age = 20;
        String degree = "Data Science and Engineering";
        double gpa = 7.85;

        // Output
        System.out.println("=== STUDENT PROFILE ===");
        System.out.println();    // empty line
        System.out.printf("Name:   %s%n", name);
        System.out.printf("Age:    %d years%n", age);
        System.out.printf("Degree: %s%n", degree);
        System.out.printf("GPA:    %.2f / 10.00%n", gpa);
        System.out.println();
        System.out.println("Status: " + (gpa >= 5.0 ? "Passing" : "Failing"));
    }
}

Compile and run:

javac Profile.java
java Profile

Output:

=== STUDENT PROFILE ===

Name:   Sergio
Age:    20 years
Degree: Data Science and Engineering
GPA:    7.85 / 10.00

Status: Passing

Two new things here. %n in printf is the platform-safe newline — it uses \r\n on Windows and \n on Linux automatically. \n always outputs a Unix newline regardless of platform. Use %n in printf and \n in println strings. "Status: " + (gpa >= 5.0 ? "Passing" : "Failing") shows the ternary operator and string concatenation with + — both work exactly as in Python.

Common errors in the first programs

Missing semicolon — most common

System.out.println("Hello")    // error: ';' expected
System.out.println("Hello");   // correct

Class name doesn’t match filename

// File: hello.java
public class Hello {    // error: class Hello should be in Hello.java

Wrong capitalisation

system.out.println("Hello");   // error: cannot find symbol
System.out.Println("Hello");   // error: cannot find symbol (Println not println)

Missing main signature

public class Test {
    public void main(String[] args) {    // error: no 'static' — won't run
        System.out.println("Hello");
    }
}

Visualise with Python Tutor

Python Tutor supports Java — select Java from the language dropdown. Paste this:

public class Demo {
    public static void main(String[] args) {
        String name = "Sergio";
        int age = 20;
        double gpa = 7.85;

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.printf("GPA: %.2f%n", gpa);

        String status = (gpa >= 5.0) ? "Passing" : "Failing";
        System.out.println("Status: " + status);
    }
}

Step through and observe three things. Each variable declaration creates a new entry in the frame — name, age and gpa appear one by one as their lines execute. When "Name: " + name is evaluated, Java concatenates the string literal and the variable’s value before passing it to println — the + operator on strings means concatenation, not addition. When the ternary (gpa >= 5.0) ? "Passing" : "Failing" is evaluated, Python Tutor shows which branch is taken and the resulting string assigned to status. This is the same program logic as Python but the execution model — one class, one main, sequential statements ending in semicolons — is what to internalise at this stage.

Quick summary

// MINIMAL PROGRAM STRUCTURE
public class ClassName {                    // class name = filename
    public static void main(String[] args) { // entry point — always this
        // code here
    }
}

// OUTPUT
System.out.println("text");       // print + newline
System.out.print("text");         // print, no newline
System.out.printf("%s %d\n", s, n); // formatted output

// FORMAT SPECIFIERS (printf)
%d   → int
%f   → double (%.2f = 2 decimal places)
%s   → String
%c   → char
%n   → platform newline (in printf)
\n   → newline (in strings)

// COMMENTS
// single line
/* multi
   line */
/** javadoc */

// ESCAPE SEQUENCES
\n → newline    \t → tab
\" → quote      \\ → backslash

// KEYWORDS IN main
public  → accessible from anywhere
static  → belongs to class, not instance
void    → returns nothing
main    → JVM entry point (exact name required)
String[] args → command-line arguments (required, even if unused)

// RULES
// 1. Filename must match class name exactly (case-sensitive)
// 2. Every statement ends with ;
// 3. { } define blocks — indentation is visual only
// 4. Java is fully case-sensitive
// 5. Everything lives inside a class

// PYTHON vs JAVA
// print("Hello")              → System.out.println("Hello");
// # comment                   → // comment
// indentation defines blocks  → { } define blocks
// no semicolons               → semicolon ends every statement
// functions at top level OK   → everything inside a class

In the next article we cover variables and data types in Java — integers, doubles, chars, Strings and how they compare to Python’s flexible type system.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *