Understanding the First Java Program
Take apart a Hello Java program: the class declaration, `public static void main`, statements, blocks and `System.out.println()`.
আপনি একটি free preview lesson দেখছেন।
Lesson Overview
In the previous lesson, we created and ran a five-line Java program by typing what we were told. That works once, but as soon as something goes wrong (a missing brace, a capital letter in the wrong place) you need to know what each part is for.
Although the program is small, it contains the fundamental building blocks of every Java program you will write: a class, a main method where execution starts, and statements inside it. In this lesson, we go through it part by part, so that nothing in it is magic any more. Some parts, such as static and String[] args, get only a first meaning now and a full explanation in later modules.
Learning Objectives
After completing this lesson, you will be able to:
- Explain the structure of a basic Java program
- Identify a class declaration
- Understand the role of the
mainmethod - Explain the basic meaning of
public,static, andvoid - Identify statements and code blocks
- Display output using
System.out.println() - Understand the roles of semicolons and braces
- Explain the execution order of a basic program
Our First Program
public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Output:
Hello Java
Program structure:
Main class
└── main method
└── output statement
Now let's look at each part.
public class Main
The first line is a class declaration:
public class Main {
Here:
public— access modifierclass— Java keywordMain— class name{— beginning of the class body
The class Keyword
class is a reserved Java keyword. It tells the compiler that we are declaring a class:
class Student {
}
Keywords have meanings defined by the language. Therefore, a keyword cannot normally be used as a variable or class name.
public
In public class Main, public is an access modifier. Access modifiers help control where a class or member can be accessed from. For now, remember:
publicindicates wider accessibility.
We will study access modifiers in detail when we learn OOP.
What Is a Class?
A class is a fundamental structure in a Java program. A class can organize data and behavior. In our first program, Main is a class. Later, we will create classes such as Student, Course, BankAccount, Order and GradeCalculator. When we learn object-oriented programming, we will understand the actual role of classes in much more detail. For now, remember:
Java code is generally organized inside classes.
Class Name: Main
Main is the name of the class, but it is not a special or mandatory class name. The following is also valid:
public class Application {
public static void main(String[] args) {
System.out.println("Application started");
}
}
In this case, the source file would be named Application.java. The name of a public class and the source file name must match: in our case, class Main lives in Main.java.
Class Naming Convention
As lesson 1.06 showed, Java class names follow the PascalCase convention: when a name contains multiple words, each word starts with a capital letter, as in GradeCalculator, BankAccount, and CourseEnrollment.
Curly Braces { }
The class body is placed inside curly braces. The opening brace { starts a block, and the closing brace } ends a block. Our program contains two nested blocks: the method block sits inside the class block, and the statement sits inside the method block.
public class Main { // Class block starts
public static void main(String[] args) { // Method block starts
System.out.println("Hello Java");
} // Method block ends
} // Class block ends
Every { needs a matching }, and the inner block must close before the outer one.
The main Method
Now look at the second important line:
public static void main(String[] args) {
This is the declaration of the main method, and the method name is main. A basic Java application usually begins execution from this method, so this line, public static void main(String[] args), is the traditional Java application entry point.
What Is a Method?
A method is a named block of related instructions. In our program, the method name is main:
public static void main(String[] args) {
System.out.println("Hello Java");
}
The instructions of the method are placed inside its curly braces, which form the method body. When the program executes, the statements inside the body run. Later, we will learn how to create our own methods.
Why Is main Important?
When a Java application is started, the runtime looks for a suitable main method and begins execution from there. For example:
public class Main {
public static void main(String[] args) {
System.out.println("First");
System.out.println("Second");
}
}
Execution:
Diagram আঁকা হচ্ছে…
Output:
First
Second
public static void
At the beginning of the main method, we have public static void. For now, understanding the basic meaning of each part is enough.
public
The method is accessible from outside the class, so it can be called when the application starts.
static
Because main is static, an object of the Main class does not need to be created before the main method can run. For now, remember:
The
mainmethod is directly available through the class for application startup.
We will understand static in more depth after learning about classes and objects.
void
void is the return type of the method. It means that the method does not return a value: our main method runs the program but does not return a result to its caller.
String[] args
Inside the parentheses of the main method, we have String[] args. This is a parameter used to receive command-line arguments: String[] is the parameter type, and args is the parameter name.
At this stage, you do not need to use arrays or command-line arguments. You only need to recognize the standard main method signature and write it exactly. We will learn about String, arrays, and parameters in later lessons.
Parentheses ( )
Parentheses appear after a method name, as in main(...); in our case, main(String[] args). Parentheses can contain a method's input or parameter declaration. They are also used when calling a method, as in println(...). This concept will become clearer when we learn methods.
Statements and the Semicolon ;
This line is a statement:
System.out.println("Hello Java");
A statement is a complete instruction in a program. Our statement displays a message in the console. Some other examples of statements, which we will learn about in later lessons, are:
int age = 30;
int total = 10 + 20;
At the end of the statement, we have a semicolon ;. Simple Java statements generally end with a semicolon, and if it is missing, as in System.out.println("Hello Java"), a compilation error occurs. Lesson 1.08 shows how to read the message the compiler gives for it.
System.out.println()
The statement System.out.println("Hello Java"); sends output to the console:
Hello Java
At a high level, you can think of it as three parts:
Systemis a class in the Java standard library.System.outrepresents standard output. In our examples, the output appears in the console.println(...)is a method that prints a value and then moves to a new line.
For example:
System.out.println("Java");
System.out.println("Foundation");
Output:
Java
Foundation
What Is the Dot . Doing?
In the expression System.out.println, the dot is used to access one member from another. Conceptually:
System
└── out
└── println()
You will see this kind of member access many times in Java code. For now, just recognize the syntax. Its meaning will become clearer after we learn about objects and methods.
"Hello Java"
"Hello Java" is a text value. In Java, text written inside double quotes is called a String literal; "Java" and "Java and OOP Foundation" are String literals too. If you remove the double quotes, Java will not treat it as text. Wrong:
System.out.println(Hello Java);
Correct:
System.out.println("Hello Java");
Java Is Case-Sensitive
Java treats uppercase and lowercase letters as different. Correct:
System.out.println("Hello");
Wrong:
system.out.println("Hello");
System.Out.Println("Hello");
because:
System ≠ system
out ≠ Out
println ≠ Println
Similarly, Main and main are not the same identifier.
Indentation
The Java compiler does not determine program structure from indentation, but indentation is extremely important for readable code. Our program indents the method four spaces inside the class and the statement four more inside the method, so its nesting is visible at a glance. Avoid writing code like this:
public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
The second version still compiles, but it is difficult to read.
Execution Order
Statements inside the main method generally execute from top to bottom, as the First/Second example above showed: each statement finishes before the next one starts. If the order of the statements changes, the order of the output also changes. Swap the two println lines and the program prints Second before First.
Complete Program Breakdown
Here is every part of the program from the start of this lesson in one table:
| Code | Meaning |
|---|---|
public | Access modifier |
class | Keyword used to declare a class |
Main | Class name |
{ } | Code block |
static | Makes the class-level method available without creating an object |
void | The method does not return a value |
main | Application entry method |
String[] args | Parameter used to receive command-line arguments |
System | Java standard library class |
out | Standard output |
println | Method used to print output |
"Hello Java" | String literal |
; | Statement terminator |
An Important Point
Not every Java class needs a main method. For example, this is a valid class:
public class Student {
}
A main method is needed only in a class that we want to use as an application entry point. A large application may contain many classes, but not all classes are application entry points.
Common Mistakes
Missing Quote
Wrong:
System.out.println("Hello Java);
The compiler sees a String literal that never ends:
Main.java:4: error: unclosed string literal
System.out.println("Hello Java);
^
Correct:
System.out.println("Hello Java");
Missing Brace
Wrong:
<!-- error -->public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
The } on the last line closes the method, so the class is never closed and the compiler runs out of code:
Main.java:5: error: reached end of file while parsing
}
^
Correct:
public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Wrong Capitalization
Wrong:
system.out.println("Hello Java");
The message looks unrelated, because Java does not know any system, so it guesses it might be a package name (lesson 3.10 explains packages):
Main.java:4: error: package system does not exist
system.out.println("Hello Java");
^
Correct:
System.out.println("Hello Java");
Incorrect main Name
Wrong:
public static void Main(String[] args) {
}
Correct:
public static void main(String[] args) {
}
Java is case-sensitive. The wrong version still compiles, because Main is a valid method name. However, the program cannot start, because Java cannot find a method named main:
Error: Main method not found in class Main, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Practice Exercise 1: Identify the Parts of the Program
Solutions: lesson 1.23.
Look at the following program:
public class CourseApplication {
public static void main(String[] args) {
System.out.println("Course started");
}
}
Identify:
- Class name
classkeyword- Method name
- Return type
- Parameter
- Output statement
- String literal
- Statement terminator
Practice Exercise 2: Predict the Output
Before running the program, write the expected output:
public class Main {
public static void main(String[] args) {
System.out.println("Java");
System.out.println("OOP");
System.out.println("Foundation");
}
}
Practice Exercise 3: Fix the Error
Fix the following program:
<!-- error -->public class Main {
public static void main(String[] args) {
System.out.println("Java")
System.out.println("Foundation");
}
}
Expected output:
Java
Foundation
Practice Exercise 4: Fix the Brace Structure
Fix the following code:
<!-- error -->public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
Practice Exercise 5: Write Your Own Program
Create the following output:
Course: Java and OOP Foundation
Java Version: 21
Status: Learning Java
Write a complete Java program using a Main class and a main method.
Knowledge Check
Question 1
What is a Java class?
Question 2
What does the class keyword do?
Question 3
If the name of a public class is Main, what should the source file be named?
Question 4
Which method is the entry point of a basic Java application?
Question 5
What do you need to remember about static at this stage?
Question 6
What does void mean?
Question 7
What is String[] args used for?
Question 8
What is a statement?
Question 9
Which symbol generally ends a simple Java statement?
Question 10
What does System.out.println() do?
Question 11
What does it mean that Java is case-sensitive?
Question 12
Does every Java class need a main method?
Knowledge Check Answers
Answer 1
A class is a fundamental structure used to organize Java code and related data or behavior.
Answer 2
The class keyword is used to declare a class.
Answer 3
The source file should be named Main.java.
Answer 4
A basic Java application usually begins execution from:
public static void main(String[] args)
Answer 5
Because main is static, an object of the Main class does not need to be created before the main method can run.
Answer 6
void means that the method does not return a value.
Answer 7
String[] args is a parameter used to receive command-line arguments.
Answer 8
A statement is a complete instruction in a program.
Answer 9
A simple statement generally ends with a semicolon ;.
Answer 10
System.out.println() prints a value to the console and moves to a new line afterward.
Answer 11
Java treats uppercase and lowercase letters as different. For example, Main and main are not the same identifier.
Answer 12
No. Only a suitable entry point is needed to start application execution. Other classes do not need to contain a main method.
Lesson Summary
- Java code is generally organized inside classes, declared with the
classkeyword. - The name of a public class and its source file must match:
Maingoes inMain.java. - A basic Java application begins execution from
public static void main(String[] args). publicis an access modifier,staticletsmainrun without creating an object, andvoidmeans the method does not return a value.- A statement is a complete instruction, and simple statements generally end with a semicolon.
- Curly braces define code blocks, such as the class body and the method body.
System.out.println()prints a value to the console and moves to a new line.- Java is case-sensitive, and statements inside
mainexecute from top to bottom.
আগে ভাবুন
শীঘ্রই আসছেCheckpoint প্রশ্ন, যেখানে lesson উত্তর দেখানোর আগে আপনাকে একটি উত্তর বেছে নিতে হবে।