Chapter 28 of 57

Objects in Java

In the previous topic, we learned about classes. A class is like a blueprint that describes what something should contain and what it should be able to do.

An object is the actual thing created from that class.

For example, if Student is a class, then John and Jason can be individual student objects. The class defines the structure, while each object contains its own data.

Creating an Object

Let's start with a simple Student class:

class Student {
    String name;
    int age;
}

Now we can create an object from this class:

Student student = new Student();

Here, student is an object of the Student class.

The new keyword creates a new object.

We can then give the object some values:

student.name = "John";
student.age = 20;

And access those values:

System.out.println(student.name);
System.out.println(student.age);

The output will be:

John
20

Creating Multiple Objects

One class can be used to create many different objects.

For example:

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 student1 and student2 were created from the same Student class, but they are two separate objects with their own data.

You can think of it like this:

Student Class
     ↓
 ┌───────────────┐
 │   student1    │ → John, 20
 └───────────────┘

 ┌───────────────┐
 │   student2    │ → Jason, 22
 └───────────────┘

Objects Can Use Methods

Objects can also use the methods defined inside their class.

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:

Student student = new Student();

student.name = "John";
student.introduce();

Output:

Hello, my name is John

The dot . is used to access members of an object.

For example:

student.name
student.introduce()

Here, name is the object's field and introduce() is its method.

Objects Have Their Own Data

This is an important concept.

Suppose we create two objects:

Student student1 = new Student();
Student student2 = new Student();

student1.name = "John";
student2.name = "Jason";

Changing student1.name does not change student2.name.

Each object has its own copy of the instance data.

So:

student1.name → John
student2.name → Jason

They come from the same class, but their data is separate.

A Real-World Example

Think about a Car class.

The class could describe what every car has:

class Car {
    String brand;
    String color;
}

We can then create different car objects:

Car car1 = new Car();
car1.brand = "Toyota";
car1.color = "Red";

Car car2 = new Car();
car2.brand = "Honda";
car2.color = "Blue";

Here, Car is the blueprint, while car1 and car2 are actual objects created from that blueprint.

The idea is similar to real life. We can have many cars, but they can all share common properties such as brand and color.

Class vs Object

It's important not to confuse these two terms.

A class describes the structure and behavior.

An object is an actual instance created from that class.

For example:

Class → Car

Objects:
car1 → Toyota, Red
car2 → Honda, Blue

A simple way to remember it is:

Class = Blueprint

Object = Instance created from the blueprint

Objects are at the heart of Object-Oriented Programming in Java. Once you understand how objects work, concepts such as constructors, encapsulation, inheritance, and polymorphism become much easier to understand.