⏱️ 9 min read • Beginner Level • Lesson 44
In Java, a class is a blueprint used to create objects, and an object is a real instance of a class. Classes define data and behavior, while objects use that data and behavior in a program.
Before learning class and objects, you should understand Java OOP concepts, Java methods, and Java variables.
A class in Java is a template or blueprint that defines the properties (data) and behaviors (methods) of objects.
class keyword.Student class is like a template or blueprint. Every student object created from it can have its own name, roll number, and marks.
class ClassName {
// fields
// methods
}
Here, ClassName should be a valid Java identifier. The class body can contain
fields, methods, constructors, blocks, and nested classes.
class Test {
int a;
private int b;
protected int c;
public int d;
void show() {
System.out.println("show");
}
}
An object is an entity that has state, behavior, and identity.
| Characteristic | Meaning | Example |
|---|---|---|
| State | Data or values of an object | id, name, salary |
| Behavior | Actions performed by an object | showDetails(), deposit(), walk() |
| Identity | Unique identification of an object | JVM identifies each object uniquely |
Creating an object from a class is known as object instantiation.
It is done using the new keyword.
ClassName objectName = new ClassName();
ClassName objectName = new ClassName();
ClassName is the class type of the reference variable.
objectName is the reference variable that stores the object's reference.
new creates a new object and allocates memory for it in the heap.
ClassName() is the constructor call that initializes the object.
objectName.
A mobile phone company may have a Mobile class.
Samsung Galaxy, iPhone, and OnePlus devices are objects created from that class.
Each object has its own color, storage, and price.
This program creates an Employee object and accesses its fields and method.
class Employee {
int id;
String name;
void showDetails() {
System.out.println(id);
System.out.println(name);
}
public static void main(String[] args) {
Employee e = new Employee();
e.id = 123;
e.name = "Ayan Khan";
e.showDetails();
}
}
Output:
123 Ayan Khan
Employee is a class.id and name are instance variables.showDetails() is an instance method.Employee e = new Employee(); creates an object.e accesses fields and method using dot . operator.
In real application development, every class does not need to have a main() method.
We can create one class and use it from another class.
class Employee {
int id;
String name;
void showDetails() {
System.out.println(id);
System.out.println(name);
}
}
public class MainClass {
public static void main(String[] args) {
Employee e = new Employee();
e.id = 123;
e.name = "Ayan Khan";
e.showDetails();
}
}
Output:
123 Ayan Khan
MainClass because it contains the main() method.
In Java, the reference variable is stored in stack memory, while the actual object is stored in heap memory.
| Instance Variable | Reference Variable |
|---|---|
| Stored inside object | Stores object reference |
| Created in Heap | Stored in Stack |
| Each object gets separate copy | Can point to same object |
We can create multiple objects of the same class. Each object has its own copy of instance variables.
class Employee {
int id;
String name;
void showDetails() {
System.out.println(id);
System.out.println(name);
}
public static void main(String[] args) {
Employee e1 = new Employee();
Employee e2 = new Employee();
e1.id = 123;
e1.name = "Ayan";
e2.id = 1234;
e2.name = "Haaziq";
e1.showDetails();
e2.showDetails();
}
}
Output:
123 Ayan 1234 Haaziq
An object alias means another reference variable points to the same object. In this case, both references access the same object in heap memory.
class Employee {
int id;
public static void main(String[] args) {
Employee e1 = new Employee();
e1.id = 101;
Employee e2 = e1; // alias
e2.id = 202;
System.out.println(e1.id);
System.out.println(e2.id);
}
}
Output:
202 202
Both e1 and e2 refer to the same Employee object in heap memory. No new object is created. So, any change made through one reference is reflected through the other.
An object without a reference variable is called an anonymous object. It is usually used when an object is needed only once.
class Employee {
void showDetails() {
System.out.println("Employee details displayed");
}
public static void main(String[] args) {
new Employee().showDetails();
}
}
Output:
Employee details displayed
| Class | Object |
|---|---|
| Class is a blueprint or template. | Object is an instance of a class. |
| No memory is allocated for instance fields when only class is defined. | Memory is allocated when object is created using new. |
| Defines fields and methods. | Uses fields and methods. |
Example: Employee class. |
Example: Employee e = new Employee(); |
What is the difference between a class and an object?
A class is a blueprint, while an object is an instance of a class.
Where are objects stored in Java?
Objects are stored in Heap memory.
Where are reference variables stored?
Reference variables are stored in Stack memory.
new keyword creates objects at runtime.Now that you understand classes and objects, learn about class members, constructors, methods, and access modifiers.
🧠 Test your understanding with a quick quiz
Topic: Class-and-objects | Language: Java
class Employee {
int id;
String name;
void showDetails() {
System.out.println(id);
System.out.println(name);
}
public static void main(String[] args) {
Employee e = new Employee();
e.id = 123;
e.name = "Ayan Khan";
e.showDetails();
}
}class Employee {
int id;
public static void main(String[] args) {
Employee e1 = new Employee();
e1.id = 101;
Employee e2 = e1;
e2.id = 202;
System.out.println(e1.id);
System.out.println(e2.id);
}
}🎉 Great job! Continue learning Java OOP step by step.