Course topics

By WebNest Studio Engineering Team

Java - Core Tutorial

Java Program Structure and First Program

Every Java application starts from a well-defined structure. Understanding this structure precisely — not just copying it — is what lets you read any Java file confidently, including ones with multiple classes, packages, and imports.

Why it matters

Every Java file you will ever read follows this structure, and the compiler enforces it strictly. Once the order of package, imports and class, and the exact shape of main, are automatic for you, error messages like "class X is public, should be declared in a file named X.java" become obvious instead of mysterious.

Hello.java source javac Hello.class bytecode java JVM finds main() output
From source file to running program: javac compiles once, the JVM runs the bytecode.

Watch: Java Program Structure and Your First Program

Prefer to watch? This video walks through the structure of a Java program and writing your first one.

Watch on YouTube

Anatomy of a Java File

A typical Java source file follows this order: an optional package declaration, optional import statements, and one or more class (or interface/enum/record) declarations. Only one public class is allowed per file, and if a public class exists, the file name must exactly match that class's name, including capitalization.

  • package declaration (optional, must be the first line if present)
  • import statements (optional, bring in classes from other packages)
  • Class declaration(s) — exactly one may be public

The main Method

Execution begins at the main method, and the JVM looks for this exact signature: public static void main(String[] args). Each keyword has a specific reason for being there: public so the JVM (outside the class) can call it, static so it can be invoked without creating an object first, void because it returns nothing to the operating system, and String[] args to receive command-line arguments.

Compilation and Execution Flow

Java source code (.java) is compiled by javac into bytecode (.class). The JVM then loads that class, locates the main method, and begins executing instructions line by line, following normal control flow (sequential execution, conditionals, loops, and method calls).

Example

A complete first program with package and structure explained

Java
package com.webnest.basics; // optional, groups related classes

public class FirstProgram {          // file must be named FirstProgram.java

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

    static void greet(String language) {
        System.out.println("Welcome to " + language + " programming.");
    }
}
Output
Hello, Webnest learners!
Welcome to Java programming.

Common Mistakes

  • Naming the file differently from the public class name (e.g. saving "Main.java" for a public class "FirstProgram") — this fails to compile.
  • Misspelling the main method signature, such as using "String args[]" incorrectly or forgetting "static" — the JVM will report "no main method found."
  • Placing the package statement anywhere other than the very first line of code.

Exercise

Write a program with a second method. greet should take a name and return a greeting, and main should print the greeting for two different names.

Expected output: Hello, Asha! then Hello, Ravi! on separate lines.

Java · starter code
public class Greeter {

    // TODO: add a static method greet(String name) that returns "Hello, <name>!"

    public static void main(String[] args) {
        // TODO: print the greeting for "Asha" and for "Ravi"
    }
}
Show hints
  • A method called directly from main without creating an object must also be static.
  • The method returns a value instead of printing it, so its return type is String, not void.
Show solution
Java · solution
public class Greeter {

    static String greet(String name) {
        return "Hello, " + name + "!";
    }

    public static void main(String[] args) {
        System.out.println(greet("Asha"));
        System.out.println(greet("Ravi"));
    }
}

Quick quiz

1. A source file contains public class Invoice. What must the file be named?

  • A. Invoice.java
  • B. invoice.java
  • C. Main.java
  • D. Any name ending in .java
Show answer

A. The file name must match the public class name exactly, including capitalisation.

2. Why is main declared static?

  • A. So it runs faster
  • B. So the JVM can call it without creating an object of the class first
  • C. So it cannot be changed
  • D. So it can return a value
Show answer

B. When the program starts no objects exist yet, so the entry point has to be callable on the class itself.

3. Where must a package statement appear?

  • A. Anywhere before the class
  • B. After the imports
  • C. As the first statement in the file
  • D. Inside the class body
Show answer

C. If present, the package declaration comes first (only comments may precede it), followed by imports, then type declarations.

Interview questions

Can a single .java file contain more than one class?

Show answer

Yes. A file can declare several classes, but at most one of them can be public, and the file must be named after that public class. The compiler still produces a separate .class file for every class in the source file.

Can the main method be overloaded?

Show answer

Yes, you can declare other methods named main with different parameter lists, and they compile fine. The JVM, however, only uses public static void main(String[] args) as the entry point; the overloads run only if your code calls them.

Key Points to Remember

  • Order in a file: package, then imports, then class declarations.
  • Only one public class per .java file, and the filename must match it.
  • The JVM always starts execution from public static void main(String[] args).

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering