Java is a powerful object-oriented programming language that focuses on the concept of classes and objects.
These two parts are the building blocks of Java programming and are important for writing structured and reusable code.
With this concept, you will understand how classes and objects helps you to think like a real programmer and design programs that model real-life situations.

What Are Classes in Java?
A class in Java acts as a blueprint or template from which object are created. It defines the structure of data and the actions that can be performed on that data.
In simple words, a class describes what an object will contain (data) and what it can do (behavior).
Components of a Class:
- Fields (Variables): These are the attributes or properties of the class. They store information or data about the object. For example, the car store has multiple fields like brand, color, and speed.
- Methods (Functions): These define the actions or behaviors the class can perform. For example, how a car can “start”, “stop”, or “display details”. These are all written as methods.
Simple Syntax of a Class:
class ClassName {
// Fields (data members)
dataType fieldName;
// Methods (behavior)
returnType methodName() {
// Method body
}
}
Example: How To Define a Class
class Car {
// Fields
String brand;
int year;
// Method
void displayDetails() {
System.out.println("Brand: " + brand + ", Year: " + year);
}
}
- In this code, The car is the class name.
- brand and year are fields that store information about a car.
- displayDetails() is a method that prints the car’s details.
So, you can see the class only defines what a car is and what it can do; it doesn’t actually represent a real car yet. For that, we need to create an object.
What is an Instance in Java?
In Java, an instance means a real and usable copy of a class. When you create an object from a class using the new keyword, that object is called an instance of the class.
What Are Objects in Java?
An object is a real-world instance of a class. If a class is a design, an object is the actual product made from that design.
Every object has its state and behavior:
- State means the data it holds (values of its fields).
- Behavior is the actions it can perform.
How to Create an Object in Java?
We can create an object in Java using the new keyword:
ClassName objectName = new ClassName();
Example: Creating and Using an Object
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Creating an object of the Car class
myCar.brand = "Toyota"; // Assigning values to fields
myCar.year = 2022;
myCar.displayDetails(); // Calling a method
}
}
Output:
Brand: Toyota, Year: 2022
Now understand this object code carefully:
- Here, we created myCar as an object of the Car class.
- It has its own values, brand = “Toyota” and year = 2022.
- When displayDetails() is called, it prints the details of that specific car.
We can create multiple objects from the same class, and each object can hold different data.
For example, one car is a Toyota (2022) and another is a Tesla (2024), but both were created from the same class design.
Features of Java Classes and Objects
1) Encapsulation: Encapsulation means wrapping data (variables) and actions (methods) inside a single unit (class).
2) Abstraction: Abstraction means showing only what is necessary and hiding unnecessary details.
3) Reusability: Once you create a class, you can reuse it to create multiple objects instead of writing the same code again.
Practical Examples of Classes and Objects
Example 1: SmartPlant, Tracking a Plant’s Growth
This example shows how a Java class can represent something real, like a plant that grows when it’s watered and gets sunlight.
It’s easy to understand and perfectly shows the concept of class, object, fields, and methods.
Code example:
class SmartPlant {
String name;
int height; // in centimeters
int waterLevel;
// Method to water the plant
void waterPlant(int amount) {
waterLevel += amount;
System.out.println(name + " has been watered with " + amount + "ml.");
}
// Method to grow the plant based on water level
void grow() {
if (waterLevel >= 100) {
height += 5;
waterLevel -= 100;
System.out.println(name + " grew 5 cm!");
} else {
System.out.println(name + " needs more water to grow!");
}
}
// Display current status of the plant
void showStatus() {
System.out.println("Plant: " + name);
System.out.println("Height: " + height + " cm");
System.out.println("Water Level: " + waterLevel + " ml");
System.out.println("-------------------------");
}
}
public class Main {
public static void main(String[] args) {
SmartPlant plant = new SmartPlant();
plant.name = "Tulsi";
plant.height = 15;
plant.waterLevel = 50;
plant.showStatus();
plant.waterPlant(60);
plant.grow();
plant.showStatus();
}
}
Output:
Plant: Tulsi
Height: 15 cm
Water Level: 50 ml
-------------------------
Tulsi has been watered with 60ml.
Tulsi grew 5 cm!
Plant: Tulsi
Height: 20 cm
Water Level: 10 ml
-------------------------
Example 2: MorningRoutine, Managing a Daily Routine
Code example:
class MorningRoutine {
String personName;
boolean brushedTeeth;
boolean hadBreakfast;
boolean exercised;
// Method to brush teeth
void brushTeeth() {
brushedTeeth = true;
System.out.println(personName + " brushed their teeth");
}
// Method to eat breakfast
void eatBreakfast() {
if (brushedTeeth) {
hadBreakfast = true;
System.out.println(personName + " had a healthy breakfast");
} else {
System.out.println(personName + " should brush their teeth first!");
}
}
// Method to do exercise
void doExercise() {
if (hadBreakfast) {
exercised = true;
System.out.println(personName + " completed morning exercise");
} else {
System.out.println(personName + " needs energy from breakfast before exercising!");
}
}
// Display final routine status
void showRoutineStatus() {
System.out.println("\n--- Morning Routine Summary ---");
System.out.println("Person: " + personName);
System.out.println("Brushed Teeth: " + brushedTeeth);
System.out.println("Had Breakfast: " + hadBreakfast);
System.out.println("Exercised: " + exercised);
}
}
public class Main {
public static void main(String[] args) {
MorningRoutine routine = new MorningRoutine();
routine.personName = "Rahul";
routine.brushTeeth();
routine.eatBreakfast();
routine.doExercise();
routine.showRoutineStatus();
}
}
Output:
Rahul brushed their teeth
Rahul had a healthy breakfast
Rahul completed morning exercise
--- Morning Routine Summary ---
Person: Rahul
Brushed Teeth: true
Had Breakfast: true
Exercised: true
- MorningRoutine class represents one person’s morning habits.
- It has fields like brushedTeeth, hadBreakfast, and exercised to store the current status.
- Methods describe what actions the person performs, brushing, eating, and exercising.
Common Mistakes to Avoid in Classes and Objects
1) Not initializing objects
Always initialize objects using the new keyword. If you only declare a variable of a class type without initializing it, your program will throw a NullPointerException.
Let’s see the example:
Car car; // Declared but not initialized
car.brand = "Honda"; // This will cause an error
Correct Way:
Car car = new Car(); // Object is properly initialized
car.brand = "Honda"; // Works fine
2) Accessing Private Variables Directly
When variables are marked as private, they cannot be accessed directly from outside the class.
This is done to protect the data from unwanted changes. Instead, you should use getter and setter methods.
For example:
Person p = new Person();
p.age = 25; // Error if 'age' is private
Right code:
class Person {
private int age;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person();
p.setAge(25);
System.out.println("Age: " + p.getAge());
}
}
3) Mixing Static and Non-Static Members
Static variables belong to the class, not to any specific object. Trying to access a non-static variable inside a static method (like main) without creating an object will cause an error.
Wrong code:
class Demo {
int number = 10;
public static void main(String[] args) {
System.out.println(number); // Error: non-static variable in static method
}
}
Right code:
class Demo {
int number = 10;
public static void main(String[] args) {
Demo d = new Demo();
System.out.println(d.number); // Works fine
}
}