📘LESSON 1
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Explain the purpose of the Java main method.
- Describe the role of the Java Virtual Machine (JVM).
- Create a simple Java class declaration.
- Run Java applications using the main() method.
- Pass arguments into the main() method.
- Explain the relationship between Java classes and the JVM.
3.2 Overview
Java applications rely on a structured execution process controlled by the Java Virtual Machine (JVM). Every Java application begins with a class declaration and starts execution from the main() method.
This lesson introduces learners to the structure of Java programs and explains how the JVM processes and executes Java applications. Learners will explore the purpose of Java classes, how the main() method works, and how command-line arguments are passed into Java applications.
These concepts form the foundation of all Java programming and are essential for developing desktop applications, enterprise systems, web applications, Android applications, and cloud-based Java solutions.
KT0101 — A Simple Java Class Declaration
A Java class is a blueprint used to define the structure and behaviour of objects in a Java application.
Every Java program is built using classes. A class contains:
- variables,
- methods,
- constructors,
- and program logic.
The class keyword is used to declare a Java class.
Structure of a Simple Java Class
public class Main {
}
Explanation of the Structure
| Component | Description |
|---|---|
| public | Access modifier allowing access from outside the class |
| class | Keyword used to declare a Java class |
| Main | Name of the class |
| {} | Body of the class containing code |
Rules for Java Class Names
Java class names:
- must begin with a letter,
- cannot contain spaces,
- should use PascalCase naming convention,
- and should clearly describe the purpose of the class.
Examples of valid class names:
- Student
- Employee
- BankAccount
- ProductManager
Examples of invalid class names:
- 1Student
- student name
- @Class
Importance of Java Classes
Classes help developers:
- organize code,
- reuse functionality,
- simplify maintenance,
- and build scalable applications.
Without classes, Java applications cannot function properly because Java is a class-based programming language.
Example of a Simple Java Class
public class Student {
}
KT0102 — The main() Method
The main() method is the starting point of a Java application.
When a Java application runs, the JVM searches for the main() method and starts executing code from that method.
Without the main() method, a Java application cannot start execution normally.
Syntax of the main() Method
public static void main(String[] args) {
}
Components of the main() Method
| Component | Purpose |
|---|---|
| public | Allows JVM to access the method |
| static | Allows method execution without creating an object |
| void | Indicates the method does not return a value |
| main | Special method name recognized by JVM |
| String[] args | Stores command-line arguments |
Why the main() Method is Important
The main() method:
- acts as the entry point of the application,
- controls application startup,
- and allows developers to execute program instructions.
Every standalone Java application requires a main() method.
Example of the main() Method
public class Main {
public static void main(String[] args) {
System.out.println("Java Application Started");
}
}
KT0103 — Running the main() Method
Java applications go through two important stages:
- Compilation
- Execution
The Java compiler converts Java source code into bytecode, and the JVM executes the bytecode.
Steps to Run a Java Program
Step 1 — Write the Java Program
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Step 2 — Save the File
The file name must match the class name.
Example:
Main.java
Step 3 — Compile the Program
javac Main.java
The compiler generates:
Main.class
Step 4 — Run the Program
java Main
Output
Hello World
Role of the JVM During Execution
The JVM:
- loads Java bytecode,
- verifies code security,
- manages memory,
- and executes Java instructions.
The JVM allows Java applications to run on multiple operating systems.
KT0104 — Passing Arguments to the main() Method
The main() method can receive input values called command-line arguments.
These arguments are passed into the String[] args parameter during execution.
Why Command-Line Arguments are Useful
Command-line arguments allow applications to:
- receive user input,
- process external data,
- and customize execution behaviour.
Example of Passing Arguments
public class Main {
public static void main(String[] args) {
System.out.println("Welcome " + args[0]);
}
}
Running the Program
java Main Bukhosi
Output
Welcome Bukhosi
Multiple Arguments Example
public class Main {
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println(args[1]);
}
}
Command
java Main Java Programming
Output
Java
Programming
KT0105 — The Java Main Class
The main class is the class containing the main() method.
The JVM starts application execution from this class.
Large Java applications may contain many classes, but only one class normally acts as the application entry point.
Characteristics of the Java Main Class
The main class:
- contains the main() method,
- controls application startup,
- coordinates program execution,
- and may call other classes and methods.
Example of a Java Main Class
public class Main {
public static void main(String[] args) {
Student learner = new Student();
learner.study();
}
}
Supporting Class Example
public class Student {
void study() {
System.out.println("Student is studying");
}
}
Real-World Example
In a banking system:
- Main.java starts the application,
- Customer.java manages customer data,
- Account.java manages accounts,
- Transaction.java processes transactions.
The main class controls the overall application flow.
3.5 Key Notes / Summary
- Java applications are built using classes.
- The main() method is the entry point of a Java application.
- The JVM executes compiled Java bytecode.
- Java programs must be compiled before execution.
- Command-line arguments are stored in String[] args.
- The main class controls application startup and execution.
- Java supports platform independence through the JVM.