As our Java programs become larger, we need a good way to organize our code. Instead of keeping everything together, Java allows us to create classes that group related data and behavior together.
A class is a blueprint or template for creating objects.
That might sound a little confusing at first, so let's use a simple real-life example.
Imagine a house blueprint. The blueprint describes things like how many rooms the house has, where the doors are, and how the house should be built. But the blueprint itself isn't an actual house. It is simply a plan from which houses can be created.
A Java class works in a similar way. It describes what an object should contain and what it should be able to do.
Creating a Class
We create a class using the class keyword.
class Student {
}
Here, Student is the name of our class.
We can place data and methods inside the class.
For example:
class Student {
String name;
int age;
}
Here, the Student class contains two pieces of information: name and age.
These are called fields or instance variables. They describe the data that a Student object can have.
Creating an Object from a Class
A class is just a blueprint. To actually use it, we create an object from the class.
For example:
class Student {
String name;
int age;
}
class Main {
public static void main(String[] args) {
Student student = new Student();
student.name = "John";
student.age = 20;
System.out.println(student.name);
System.out.println(student.age);
}
}
Output:
John
20
Here, Student is the class, while student is an object created from that class.
The statement:
Student student = new Student();
creates a new Student object.
We can then use the object to access its fields:
student.name = "John";
student.age = 20;
Why Do We Need Classes?
Classes become especially useful when our program deals with many related pieces of information.
Imagine we are creating a student management application. Every student may have a name, age, roll number, and marks.
Instead of creating unrelated variables everywhere:
String student1Name = "John";
int student1Age = 20;
String student2Name = "Jason";
int student2Age = 21;
we can create a Student class:
class Student {
String name;
int age;
}
Then create multiple objects:
Student student1 = new Student();
Student student2 = new Student();
student1.name = "John";
student1.age = 20;
student2.name = "Jason";
student2.age = 21;
Now each object represents a different student while following the same structure defined by the Student class.
Classes Can Contain Methods
A class doesn't only store data. It can also contain methods that describe what an object can do.
For example:
class Student {
String name;
void introduce() {
System.out.println("Hello, my name is " + name);
}
}
Now we can create an object and call its method:
class Main {
public static void main(String[] args) {
Student student = new Student();
student.name = "John";
student.introduce();
}
}
Output:
Hello, my name is John
The Student class contains both data (name) and behavior (introduce()).
This is one of the important ideas behind Object-Oriented Programming.
A Class Can Create Many Objects
One of the biggest advantages of a class is that we can create many objects from the same class.
class Student {
String name;
int age;
}
class Main {
public static void main(String[] args) {
Student student1 = new Student();
student1.name = "John";
student1.age = 20;
Student student2 = new Student();
student2.name = "Jason";
student2.age = 22;
System.out.println(student1.name);
System.out.println(student2.name);
}
}
Output:
John
Jason
Both objects were created from the same Student class, but each object has its own data.
You can think of it like this:
Student Class
/ \
/ \
Student 1 Student 2
John Jason
20 22
The class defines the structure, while each object contains its own values.
Class vs Object
This is one distinction you should remember:
Class → Blueprint
Object → Actual thing created from the blueprint
For example:
Class: Student
Objects:
student1 → John, 20
student2 → Jason, 22
The Student class describes what a student object should contain. student1 and student2 are actual objects created from that class.
Classes are one of the most important concepts in Java because they form the foundation of Object-Oriented Programming. As we continue, we'll learn how constructors, methods, inheritance, encapsulation, and other OOP concepts work with classes and objects.