3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Create Java classes correctly.
- Write Java source code.
- Compile Java programs.
- Execute Java applications.
- Interpret program output.
3.2 Overview
Java applications are built using classes that contain methods, variables, and program logic. Developers create Java source files, compile them into bytecode, and execute them using the Java Virtual Machine (JVM).
This lesson introduces learners to the practical process of creating and running Java classes using Java development tools.
Creating and running Java classes is important in:
- software development,
- enterprise programming,
- Java application development,
- and object-oriented programming.
Understanding Java class creation and execution is important because it forms the foundation of Java software development.
PA0401 — Creating a Java Class
A Java class is a blueprint that contains program instructions and methods.
Example Java Class
Java
public class Main {
}
Rules for Java Class Creation
|
Rule |
Description |
|---|---|
|
File name matches class name |
Main.java |
|
Use class keyword |
Defines class |
|
Use braces |
Defines class body |
Practical Activity
Learners must:
- create a Java class,
- save the file correctly,
- and apply Java naming conventions.
PA0402 — Writing the main() Method
The:
Plain text
main()
method is the entry point of a Java application.
Example
Java
public static void main(String[] args) {
System.out.println(“Hello Java”);
}
Components of 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:
- create the main() method,
- type Java statements,
- and save the program.
PA0403 — Compiling Java Source Code
Compilation converts Java source code into bytecode.
Compilation Command
Plain text
javac Main.java
Result of Compilation
Plain text
Main.class
Practical Activity
Learners must:
- open Command Prompt,
- compile Java source files,
- and identify generated class files.
PA0404 — Running Java Programs
Compiled Java applications are executed using the JVM.
Execution Command
Plain text
java Main
Example Output
Plain text
Hello Java
Practical Activity
Learners must:
- execute Java programs,
- observe console output,
- and identify execution errors.
PA0405 — Modifying and Re-running Java Programs
Java programs can be edited, recompiled, and executed repeatedly.
Example Modified Program
Java
public class Main {
public static void main(String[] args) {
System.out.println(“Welcome to Java Programming”);
}
}
Practical Activity
Learners must:
- modify Java source code,
- save changes,
- recompile programs,
- and run updated applications.
PA0406 — Identifying Compilation and Runtime Errors
Errors may occur during compilation or execution.
Common Error Types
|
Error Type |
Description |
|---|---|
|
Syntax Error |
Incorrect program structure |
|
Runtime Error |
Error during execution |
|
Logical Error |
Incorrect output |
Practical Activity
Learners must:
- identify compilation errors,
- correct syntax mistakes,
- and re-run programs successfully.
3.5 Key Notes / Summary
- Java programs are created using classes.
- The main() method is the program entry point.
- javac compiles Java source files.
- java executes compiled applications.
- Java programs can be modified and recompiled.
- Compilation and runtime errors must be corrected.