Module 1 ยท Java from zero Lesson 1 of 15
Your first class and the main method
Everything in Java lives inside a class, and a program starts at public static void main(String[] args). The words look strange because they are precise, not decorative.
The concept
A Java program is a class with a main method, and the JVM runs that method last โ everything you write is called from somewhere, and main is where the calling starts. public static void main(String[] args) is a mouthful you can type for a week until it becomes automatic.
Compile with javac Hello.java, which produces Hello.class, then run java Hello โ compile once, run anywhere the JVM exists. That two-step is the reason Java ended up in banks and service companies, and it is the first thing an interviewer asks a fresher to describe.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Asha");
}
}
// The file name MUST match the public class: Hello.java
// Compile: javac Hello.java Run: java HelloCommon mistakes
The mistakes are a file name that does not match the public class name, and a forgotten semicolon at the end of a statement. Both produce messages that name the line and the file, so read the first error rather than the tenth โ one mistake in Java often produces a cascade.
Worked example
A file named Hello.java containing public class Hello with a main method prints one line; rename the file without renaming the class and it will not compile. Java enforces the rule that the public class and the filename agree.
Your practice task
Write, compile with javac and run with java โ not in an IDE โ and then send the same program a command-line argument and print it back.
Do this before moving on. Reading a solution teaches the shape of it; typing it teaches you.
Key takeaway
A Java program is a class with a main method, and the JVM runs that method last โ everything you write is called from somewhere, and main is where the calling starts.
Keep your place in this course
A free account saves which lessons you have finished and unlocks the certificate. The lesson itself stays free to read.
Create free account or sign inCourse complete
You finished every lesson.
Your certificate is ready, and the projects you built are the proof to attach to an application.
Almost there
15 lessons left of 15
Finish every lesson to unlock the certificate. Each one ends in a practice task โ do them and the final project brief writes itself.