Skip to content
AgileLogics
5 min read

Your First Java Program

Prerequisites: Installing the JDK

Create a file named Main.java — the filename must exactly match the public class name inside it:

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

Common mistakes

  • Naming the file something other than Main.java when the class is public class Main — Java requires the two to match exactly.
  • Forgetting static on main — the JVM calls main without first creating an instance of the class, so it must be static.

Practice

Change the program to print your own name instead of "Hello, world!".

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