Concept Explanation
Now you have a JDK and an IDE. This topic connects them: you will go from an empty folder to text printed on screen, and understand every piece you typed.
Every Java program needs exactly one entry point — the main method. When you press Run, the JVM looks for main, and starts there. No main, no program.
Step by Step
In IntelliJ, right-click the src folder, choose New → Java Class, and name it Welcome. Then type:
public class Welcome {
public static void main(String[] args) {
System.out.println("My first Java application");
System.out.println("Compiled and running");
}
}
Press Shift + F10. The console prints both lines.
What Each Keyword Does
- public — visible from anywhere; the JVM must be able to reach
main. - class Welcome — the container. The file must be named
Welcome.java. - static — callable without creating a
Welcomeobject first. - void — returns nothing.
- main — the exact name the JVM searches for.
- String[] args — values passed in from the command line.
What Happens When You Press Run
javaccompilesWelcome.javaintoWelcome.class(bytecode).- The
javalauncher starts a JVM. - The JVM loads
Welcome.classand callsmain. - Your text reaches the console.
The same thing by hand:
javac Welcome.java
java Welcome
Note you run java Welcome — the class name, not Welcome.class.
Common Mistakes
- Filename mismatch:
public class Welcomemust live inWelcome.java. Java is strict about this. - Misspelling main:
Main,mian, ormain()withoutString[] argsall produce "main method not found". - Running the file, not the class:
java Welcome.classfails. Drop the extension.
Best Practices
- PascalCase class names:
Welcome,OrderProcessor— neverwelcome. - Keep main thin: As programs grow,
mainshould call other methods rather than hold all the logic. - Run early and often: Compile after every few lines so errors stay small and findable.
Practice Exercises
- The Introduction: Write a class
AboutMethat prints your name, your city, and why you are learning Java on three lines. - The Manual Route: Compile and run the same class from the terminal with
javacandjavainstead of the IDE button. - The Break-It Drill: Rename the class to
Welcome2but leave the file asWelcome.java. Read the error, then fix it.