3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Declare a simple Java class.
- Declare the
main()method correctly. - Run a Java application successfully.
- Use Java syntax correctly in a basic program.
- Display output using Java instructions.
3.2 Overview
The main() method is the entry point of every Java application. Java programs begin execution inside the main() method. Before running a Java application, learners must understand how to create Java classes and structure Java programs correctly.
This lesson introduces learners to the practical process of creating a Java class, declaring the main() method, and running Java applications.
Understanding the main() method is important because:
- every Java application starts from it,
- it controls program execution,
- and it forms the foundation of Java programming.
PA0101 — Declare a Simple Java Class
A Java class is a blueprint used to create Java applications and objects.
Example Java Class
Java
public class FirstProgram {
}
Rules for Declaring a Java Class
|
ule |
Description |
|---|---|
|
Use class keyword |
Declares a class |
|
File name matches class name |
FirstProgram.java |
|
Use braces |
Defines class body |
Practical Activity
Learners must:
- create a Java class,
- save the class correctly,
- and apply Java naming conventions.
PA0102 — Declare a main() Method
The main() method is the starting point of a Java application.
Example main() Method
Java
public class FirstProgram {
public static void main(String[] args) {
}
}
Components of the main() Method
|
Component |
Purpose |
|---|---|
|
public |
Access modifier |
|
static |
Shared method |
|
void |
No return value |
|
main |
Method name |
|
String[] args |
Command-line arguments |
Practical Activity
Learners must:
- declare the
main()method, - use correct Java syntax,
- and save the program correctly.
PA0103 — Run a main() Method
Java applications are executed using the Java Virtual Machine (JVM).
Example Program
Java
public class FirstProgram {
public static void main(String[] args) {
System.out.println(“Hello Java!”);
}
}
Compilation Command
Plain text
javac FirstProgram.java
Execution Command
Plain text
java FirstProgram
Expected Output
Plain text
Hello Java!
Practical Activity
Learners must:
- compile the Java source file,
- run the Java program,
- and verify successful output.
3.5 Key Notes / Summary
- Java programs are written inside classes.
- The
main()method is the entry point of a Java application. - Java source files use the
.javaextension. javaccompiles Java source code.javaexecutes compiled Java applications.System.out.println()displays output to the console.