Free degree-level programming lessons for careful independent study.

Degree Level Programmes · Programming 1 · Lesson 5

The Main Method

Understand why Java needs a precise starting point before any other code can run.

Lesson Overview

Understand why Java needs a precise starting point before any other code can run.

Portfolio focus: Type a class with two print statements in main.

ConceptProgram entry point
Run fileMainMethodEntryPointDemo.java
BaselineStart here
Evidence3 tasks

Starter: think before typing

Before running this program entry point example, find the line where the main idea becomes active. Write a prediction: what must already be true for that line to work, and what should be different after it runs? The checked run ends with `Start here`; predict how the focus line helps produce that evidence.

Learning Objectives

  • Identify the Java main method.
  • Explain why execution needs an entry point.
  • Separate class structure from the first executed statement.
  • Predict which output appears first.

Learning Outcomes

  • By the end of the lesson, you can identify the Java main method.
  • By the end of the lesson, you can explain why execution needs an entry point.
  • By the end of the lesson, you can separate class structure from the first executed statement.
  • By the end of the lesson, you can predict which output appears first.

Why this idea exists

A program needs a defined starting point because code can contain many classes and methods. Without an entry point, Java would not know which behaviour should happen first.

Java inherited the idea of a named entry point from earlier systems programming traditions, while placing it inside a class-based structure. `public static void main(String[] args)` is therefore a convention that connects the operating environment to your Java code.

In the course arc, the main method is the bridge between a file full of definitions and a program that actually begins executing. Later, as programs grow, main should become a small coordinator rather than the place where every decision lives.

Deep dive

Mechanism in this example

The important mechanism is visible around `public class MainMethodEntryPointDemo {`. Read it as a concrete move in the program, not as decorative syntax: identify what value, object, branch, call or boundary is being created at that point.

Design pressure

In the course arc, the main method is the bridge between a file full of definitions and a program that actually begins executing. Later, as programs grow, main should become a small coordinator rather than the place where every decision lives.

Failure mode to watch

For The Main Method, deliberately disturb the assumption behind `public class MainMethodEntryPointDemo {`: use an awkward value, missing input, wrong order of calls or boundary case. The useful question is how that disturbance exposes a weakness in the program entry point design.

Extension step

Extend the example by doing this: Predict the order of output. The point is to make one small change that forces you to revisit the concept, rather than adding unrelated features.

Portfolio standard

The portfolio note should not repeat the lesson wording. It should show the edited code, the run result, and your own explanation of this evidence: move one line and explain why the order changes.

Lesson visual

A start-line graphic for a Java program with arrows from launch to public static void main and then to the first print statement.
A start-line graphic for a Java program with arrows from launch to public static void main and then to the first print statement.Download visual

Type this and run it

Create MainMethodEntryPointDemo.java, type the program, and run it before changing anything. This section is about reproducing the checked baseline.

public class MainMethodEntryPointDemo {
  public static void main(String[] args) {
    System.out.println("Start here");
  }
}

Build and run it with:

javac MainMethodEntryPointDemo.java && java MainMethodEntryPointDemo

Expected baseline: Start here

Run the code in your browser

Use the editor as an experiment surface. First run the checked version, then make one small change to the part of the program that demonstrates program entry point and compare the new behaviour with the reference output.

Line-by-line explanation

Read the code as a sequence of responsibilities. The focus line for this lesson is public class MainMethodEntryPointDemo {; the surrounding lines prepare it, use its result or make the behaviour observable.

public class MainMethodEntryPointDemo {

This names the runnable class for the The Main Method example, giving the compiler and JVM one clear unit to build.

public static void main(String[] args) {

This is the program entry point. In The Main Method, it keeps the demonstration of program entry point in one traceable starting script.

System.out.println("Start here");

This prints "Start here" as the observable evidence for The Main Method. The output lets the learner check whether the program entry point idea behaved as predicted.

}

This closes the innermost The Main Method block, so the immediately preceding method, branch or loop has finished.

}

This closes the outer The Main Method structure, returning the reader to the surrounding class or file.

Worked example

From code to explanation

Problem: Use The Main Method to complete a small portfolio-quality step: Type a class with two print statements in main.

Method: Locate the line `public class MainMethodEntryPointDemo {`, explain the exact role it plays, then decide what you would change to extend the example without changing the whole program.

Reveal worked answer

The checked run should produce `Start here`. A strong answer links the result back to program entry point: what was created, selected, stored, called or protected, and why that matters for the portfolio task.

Trace the program

Before: Before the key operation, identify the relevant value, object, branch or resource that the program entry point concept depends on.

During: Trace `public class MainMethodEntryPointDemo {` as the The Main Method example executes. Say whether that operation creates data, checks a condition, calls behaviour, stores information or crosses a boundary.

After: Compare the run with the expected evidence: `Start here`.

Change: Now predict the order of output, run again, and explain the smallest reason the behaviour changed.

Common misconception

A common mistake in the main method is treating the example as a finished answer. For program entry point, the important question is narrower: which operation carries the idea, what does it make possible, and what would break if you changed it carelessly?

Quick checks

1. In this The Main Method example, what is the best reason to focus on `public class MainMethodEntryPointDemo {`?

2. Which evidence is strongest after you edit and rerun this example?

How to study this lesson

For The Main Method, predict how program entry point changes the run before you press Run.

Use the first portfolio task as your main edit: Type a class with two print statements in main.

Use the second task as your variation: Predict the order of output.

Finish with evidence, not a diary entry: Move one line and explain why the order changes.

Portfolio Practice

  1. Type a class with two print statements in main.
  2. Predict the order of output.
  3. Move one line and explain why the order changes.

Final self-check

Can you explain the key operation?

Explain the line identified in the quick check in one or two sentences. Your answer should say what it does before the output Start here appears.

Can you justify the portfolio evidence?

Your evidence should include the original run, one edited run, and a short note explaining how the edit affected program entry point.

Study route

Practise program entry point by predicting the Java example, typing it, running it in the browser, tracing the result and saving portfolio evidence.

Next, move into Calling and Returning with Methods and carry forward one improvement from this lesson into the next program.