How to Write Your First Java Application
Example of the first Java application
Specialized IDEs like IntelliJ IDEA are commonly used for Java development — we'll cover creating a project in IntelliJ IDEA in a later lesson. But for your first program, any text editor (Notepad, Notepad++) works: write the code and save the file with a .java extension.
Here's your first Java program, in a file named MyFirstApp.java:
public class MyFirstApp {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
} The program prints Hello, World! to the console.
The main() line explained
The line public static void main(String[] args) is the program's entry point — execution starts here. Let's break down each word:
| Part | What it means |
|---|---|
public | Access modifier: the method is visible everywhere so the JVM can call it |
static | Belongs to the class, not an object — it can be called without creating an instance |
void | The method returns nothing |
main | The method name. This is what the JVM looks for when starting the program |
String[] args | Parameter: an array of strings holding command-line arguments |
Key rules for writing Java applications
- A source file is a text file with the .java extension, containing one or more class definitions.
- The name of a public class must match the filename (class
MyFirstApp→ fileMyFirstApp.java). - All code lives inside a class, between the curly braces
{ }. - Program execution always starts with the main() method.
- Java is case-sensitive:
Mainandmainare different names. - Statements end with a semicolon
;. - Extra spaces, tabs, and line breaks are ignored by the compiler.
- A class without a
main()method will compile, but thejavalauncher can't run it.
The modern simplified main (Java 25+)
Since Java 25, you can write a first program more concisely — without a wrapper class and without public static:
void main() {
IO.println("Hello, World!");
} This is a learning-focused feature. But the classic public static void main(String[] args) is still everywhere — in real projects, documentation, and interviews — so you need to know it. This course uses the classic syntax.
Frequently asked questions
What does public static void main(String[] args) mean?
It's the program's entry point. public makes the method accessible to the JVM, static lets it be called without creating an object, void means it returns nothing, main is the name the JVM looks for, and String[] args holds command-line arguments.
Why must the filename match the class name?
The compiler requires it for a public class: class MyFirstApp must live in a file named MyFirstApp.java. Java is also case-sensitive, so the names must match exactly.
What can I write Java code in?
For a first program, any text editor (Notepad, Notepad++) works — just save the file with a .java extension. For real work, an IDE like IntelliJ IDEA is much more convenient.
What happens if a class has no main() method?
The class compiles into bytecode, but the java launcher can't run it: there's no entry point to start from. The main() method is the required entry point for a runnable program.
Why does main need the static keyword?
It lets the JVM call main() without creating an object of the class. When the program starts there are no instances yet, so the entry point must be static.
Video Explanation
Prefer video format? Watch this lesson with examples and explanations.
Comments