Java is a programming language, and like any language, it has rules on how we write sentences
(called code). When we write Java code, we need to follow specific rules for the program to
work. Let’s break down the basic structure of a Java program:
1. Class Definition
In Java, everything we do happens inside a class. A class is like a blueprint or a template for
creating objects. We start by defining a class using the keyword class.
class MyProgram {
// Code goes here
}
2. Main Method
Every Java program starts from a special method called the main method. This is where the
program begins running. It looks like this:
public static void main(String[] args) {
// Code goes here
}
public: This means the method is available to be used anywhere.
static: This means the method can be run without creating an object.
void: This means the method doesn’t return any value.
String[] args: This is a way to pass information (called arguments) into the program
when you run it.
Classroom Analogy for public static void main(String[] args)
Imagine you're in a classroom, and there's a teacher who is going to start the lesson for the
whole class. The teacher has a set of instructions they will give to the students to start the lesson.
In Java, public static void main(String[] args) is like this set of instructions from the
teacher to start the program.
Breaking it Down:
1. public = Open Door for Everyone (Accessible)
In the classroom, there is a door that is open to everyone (students, visitors, etc.). public
is like this open door—it means anyone can access it, even from outside the classroom.
In Java:
o public means that the main method can be accessed from anywhere, and the
program can start from here.
2. static = Teacher Doesn’t Need a Student to Start
In a classroom, the teacher can start the lesson without needing a student to begin.
The teacher is independent and doesn't depend on a student to do the first action.
In Java:
o static means that the main method doesn't need to create any objects (students)
to start—it’s the first point of action for the program, and it starts the lesson on
its own.
3. void = No Homework or Test (No Return Value)
In the classroom, the teacher might say, "I’m going to start the lesson, but you don’t need
to write anything down or submit anything back to me." There's no homework or test
that needs to be returned.
In Java:
o void means that the main method doesn't return any value. It’s just starting
things up, and no information is sent back from this method to anywhere else.
4. main = Starting Point (The Lesson Begins)
In the classroom, the teacher begins the lesson at a specific point in time, and this is
where all activities in the class start.
In Java:
o mainis the starting point of the Java program. It’s the first method that runs
when you execute your program.
5. String[] args = Instructions for the Class (Students)
Imagine the teacher gives instructions (like a note or message) to the students before
starting the lesson. These instructions are optional, but they help guide the lesson.
In Java:
o String[] args represents instructions or data that can be passed into the
program when it starts. The students (or objects) can follow these instructions,
but it’s optional. If not given, the program can still run without them.
Putting it All Together:
public static void main(String[] args) is like the teacher starting the lesson with a set
of instructions (args). The teacher (static) doesn't depend on any student (objects) to begin the
lesson, and the teacher gives access to everyone (public), but no homework or test is required
from the class (void).
So, in simple terms:
public = Open door for everyone to access.
static = Teacher can start without any student.
void = No homework or test to submit.
main = Start of the lesson.
String[] args = Optional instructions for the class.
Example in Classroom Terms:
public class Classroom {
public static void main(String[] args) {
[Link]("Class has started! Welcome!");
// If any instructions were passed, the teacher could use them here
}
}
In this example, main starts the lesson, and the teacher (program) gives a welcome message. If
there were any instructions (args), the teacher could use them during the lesson, but it’s not
required.
_______________________________________________________________________
3. Statements
Inside the main method, we write statements (like commands) that tell the computer what to do.
For example:
[Link]("Hello, World!");
This line prints "Hello, World!" on the screen. It’s a statement that tells the computer to show a
message.
4. Variables
Variables are used to store information like numbers, text, or other data. You can define a
variable like this:
int age = 14;
String name = "John";
int: This is a type of variable that stores whole numbers.
String: This is a type of variable that stores text.
5. Braces {}
In Java, we use curly braces { } to group things together. For example, the code inside the class
and the main method is written inside curly braces.
Example Code
Here is an example of a simple Java program:
class MyProgram {
public static void main(String[] args) {
[Link]("Hello, World!");
int age = 14;
String name = "John";
[Link]("My name is " + name + " and I am " + age + " years
old.");
}
}
Key Parts:
class MyProgram: This defines a class called MyProgram.
main method: This is where the program starts.
[Link]: This is used to print messages on the screen.
Variables: age and name are used to store data.
Summary:
A Java program is made up of classes and methods.
The main method is where the program starts.
We use curly braces {} to group code.
We write statements inside the main method to tell the computer what to do.