Skip to content
Learn Netverks

Lesson

Step 6/36 17% through track

class-main-method

Class and main method

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~2 min
Level
beginner

This lesson

This lesson teaches Class and main method: the syntax, APIs, and habits you need before advancing in Java.

Teams ship Class and main method on every Java codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Class and main method in contexts like: Spring Boot APIs, banking systems, Android (with Kotlin), and batch/data pipelines on the JVM.

Write Java with a public class (lessons use Main), click Run on server—the dev runner runs javac then java; fix compile errors from stderr (LEARNING_RUNNER_ENABLED=true). Also keep the public class name matching the file (Main) so javac succeeds.

When you can explain the previous lesson's ideas without copying starter code.

Every standalone Java program starts in a class. The JVM looks for public static void main(String[] args) as the entry point—similar to how PHP scripts start at the top of a file, but Java requires an explicit method signature.

Minimal program structure

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

Public class name must match the filename. Save this as Main.java. A file cannot declare two public top-level classes.

Reading the main signature

  • public — JVM can call it from outside the class
  • static — runs without creating an instance of Main
  • void — returns nothing
  • String[] args — command-line arguments (often empty in the playground)

Important interview questions and answers

  1. Q: Why must main be static?
    A: The JVM invokes main before any object exists; static methods belong to the class, not an instance.
  2. Q: Can main be in any class?
    A: Yes, but conventionally in a class named after the app; the playground expects Main.
  3. Q: What happens if the class name does not match the file?
    A: javac fails with a compile error.

Self-check

  1. What three keywords appear before void main?
  2. Why is the class named Main in this track?

Tip: The public class name must match the filename—Main.java requires public class Main. Fix that before debugging logic errors.

Interview prep

Why must main be static?

The JVM calls main before any instance of the class exists—static methods belong to the class itself.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • main signature parts?
  • Multiple classes per file?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump