Classes and objects are the foundation of object-oriented programming in Java. If OOP is the overall design approach, then classes and objects are the building blocks you use to put that approach into practice.
A class defines what an object should contain and what it should be able to do. An object is the actual runtime entity created from that class.
A useful way to remember the relationship is: class = definition, object = instance.
Why Do We Need Classes?
Consider an application that manages employees. Every employee may have a name, employee ID, department, and salary. Employees may also perform actions such as displaying their information or calculating a salary-related value.
Without classes, you could store these values in separate variables. But as the number of employees grows, managing those variables becomes increasingly difficult.
A class allows you to define the structure once and create as many employee objects as the application needs.
What Is a Class?
A class is a user-defined type that describes the data and behavior that objects of that type can have.
A class can contain fields, methods, constructors, and other members. Think of it as a design or blueprint rather than the actual object itself.
class Employee {
String name;
int employeeId;
double salary;
void displayDetails() {
System.out.println(name);
System.out.println(employeeId);
System.out.println(salary);
}
}
Here, Employee is a class. It describes three pieces of data—name, employeeId, and salary—and one behavior, displayDetails().
At this point, no employee object has been created. We have only defined what an employee object should look like.
Remember: Defining a class does not automatically create an object. The class is the definition; an object must be created separately.
What Is an Object?
An object is a runtime instance of a class. When an object is created, it can hold its own values for the fields defined by the class and can use the methods provided by that class.
For example, if Employee is the class, then employee1 and employee2 can be two different objects of that class.
Employee employee1 = new Employee(); Employee employee2 = new Employee();
The keyword new creates a new object. Each object has its own instance state.
Creating and Using Objects
Let's create an employee object and assign values to its fields.
class Employee {
String name;
int employeeId;
double salary;
void displayDetails() {
System.out.println(name);
System.out.println(employeeId);
System.out.println(salary);
}
}
public class Main {
public static void main(String[] args) {
Employee employee = new Employee();
employee.name = "Riya";
employee.employeeId = 101;
employee.salary = 55000.0;
employee.displayDetails();
}
}
The statement Employee employee = new Employee(); creates an object of the Employee class.
The left side declares a reference variable named employee, while the right side creates the actual object.
The dot operator . is then used to access the object's fields and methods.
Class vs Object
| Class | Object |
|---|---|
| Defines a type | Is an instance of that type |
| Acts as a blueprint or definition | Represents an actual runtime entity |
| Describes possible state and behavior | Contains actual state |
| Does not represent one specific entity | Represents a specific entity |
| Example: Employee | Example: employee1 |
Creating Multiple Objects
One of the biggest advantages of a class is that you can create multiple objects from the same definition.
Employee employee1 = new Employee(); employee1.name = "Riya"; employee1.employeeId = 101; employee1.salary = 55000.0; Employee employee2 = new Employee(); employee2.name = "Arjun"; employee2.employeeId = 102; employee2.salary = 62000.0;
Both objects belong to the same Employee class, but they contain different values.
This is an important OOP idea: one class can define the structure for many independent objects.
Understanding Object State
Every object can have its own state. In the previous example, employee1 has one name and salary, while employee2 has another.
employee1.name = "Riya"; employee1.salary = 55000.0; employee2.name = "Arjun"; employee2.salary = 62000.0;
Changing employee1.salary does not change employee2.salary. Each object maintains its own instance data.
Important: Objects created from the same class share the class definition, but instance variables hold state separately for each object.
Object Behavior Through Methods
Objects are not just containers for data. They can also perform actions through methods.
class Calculator {
int number;
void doubleValue() {
number = number * 2;
}
}
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.number = 10;
calculator.doubleValue();
System.out.println(calculator.number);
}
}
The object begins with number equal to 10. When doubleValue() is called, the object changes its own state to 20.
This illustrates the deeper idea behind objects: they can combine state and behavior into one meaningful unit.
Reference Variables and Objects
A common beginner mistake is assuming that the variable on the left side of new is the object itself.
Employee employee = new Employee();
Here, employee is a reference variable. The new Employee() expression creates an object, and the reference variable refers to that object.
This distinction becomes especially important when you start working with multiple references, method parameters, arrays of objects, and object assignment.
Two References Can Point to the Same Object
Consider the following example:
Employee employee1 = new Employee(); employee1.name = "Riya"; Employee employee2 = employee1; employee2.name = "Arjun"; System.out.println(employee1.name);
The output is Arjun. Why? The assignment employee2 = employee1 does not create a second employee object. Both reference variables refer to the same object.
Remember: Assigning one object reference to another copies the reference, not the object. If two references point to the same object, a change made through one reference can be visible through the other.
Class and Object in a Real Application
Imagine an e-commerce application. A Product class might define product ID, name, price, and methods for displaying product information.
class Product {
int productId;
String name;
double price;
void displayProduct() {
System.out.println(name + " - " + price);
}
}
The application can then create many product objects:
Product product1 = new Product(); Product product2 = new Product(); Product product3 = new Product();
All three objects follow the same structure, but each can represent a different product. This is how a simple class definition can become the foundation for thousands of real application entities.
Common Beginner Mistakes
- Confusing a class with an object.
- Thinking that declaring a reference variable automatically creates an object.
- Forgetting the new keyword when an object needs to be created.
- Assuming that assigning one reference to another creates a separate object.
- Thinking that all objects created from a class automatically share instance variable values.
Best Practices
- Give classes meaningful names that represent real concepts or clear responsibilities.
- Keep a class focused instead of making it responsible for unrelated operations.
- Use objects to model meaningful entities and responsibilities in the application.
- Prefer controlled access to object state as applications become more complex.
- Understand the difference between an object and a reference before moving into advanced Java topics.
Interview Insights
A frequently asked interview question is: “What is the difference between a class and an object?”
A concise answer is: A class is a definition that describes the structure and behavior of a type, whereas an object is a runtime instance of that class.
Interviewers may also ask whether multiple objects can be created from one class. The answer is yes. A single class can be used to create many independent objects, each with its own instance state.
Quick Learning Check
Before moving to constructors, make sure you can answer these questions:
- What is a class?
- What is an object?
- What does the new keyword do?
- Why can multiple objects be created from one class?
- What is the difference between an object and a reference variable?
- What happens when two reference variables point to the same object?
Final Takeaway
Classes and objects form the foundation of Java's object-oriented programming model. A class defines the structure and behavior of a type, while an object is a real runtime instance that carries its own state and can perform behavior through methods. Once you clearly understand the relationship between a class, an object, and a reference variable, many later Java concepts become much easier to understand because they build directly on this foundation.
