DEV Community

Cover image for Class And Objects in Java
Vigneshwaran V
Vigneshwaran V

Posted on

Class And Objects in Java

1.What is a Class?

In Java, a class is a blueprint, template, or prototype used to create objects. It is the foundational building block of Object-Oriented Programming (OOP) that defines the data structure and actions that the resulting objects will possess.

A class is a logical entity, meaning it does not occupy any physical memory space until an object is actively instantiated from it.

Example:
Think of a class as an architectural blueprint for a house. The blueprint itself is not a house—it just details where the doors, windows, and floors go. When you actually build a physical house using that blueprint, you are creating an object (an instance of the class). You can build hundreds of unique houses using that single template.

A class defines:

  • Data => variables/fields
  • Behavior => methods

For Example:
Imagine we want to represent a student.

class Student {

    String name;
    int age;

    void study() {
        System.out.println(name + " is studying");
    }
}
Enter fullscreen mode Exit fullscreen mode

Here Student is a class.
It tells us that every Student object can have:

name
age
study()
Enter fullscreen mode Exit fullscreen mode

But the class itself is just the design. We haven't created an actual student yet.

2.What is Object

In Java, an object is a self-contained unit that serves as an instance of a class, combining both data (state) and actions (behavior).

A class acts as a template or blueprint, while an object is the physical realization built from that blueprint. When you instantiate an object, the Java Virtual Machine (JVM) allocates memory for it on the heap space.

In Simple words:
Anything which Existence in the Real World is known as Object.

We create an object using new Keyword:

Student s1 = new Student();
Enter fullscreen mode Exit fullscreen mode

Here:

Student        Class
s1             Reference variable
new Student()  Object creation
Enter fullscreen mode Exit fullscreen mode

Then we can give data to the object:

s1.name = "Vicky";
s1.age = 22;

s1.study();
Enter fullscreen mode Exit fullscreen mode

Output:

Vicky is studying
Enter fullscreen mode Exit fullscreen mode

Complete Example

class Student {

    String name;
    int age;

    void study() {
        System.out.println(name + " is studying");
    }
}

class Main {
    public static void main(String[] args) {

        Student s1 = new Student();

        s1.name = "Vicky";
        s1.age = 22;

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

        s1.study();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Vicky
22
Vicky is studying
Enter fullscreen mode Exit fullscreen mode

Why do we need objects?

Suppose we want to store information about two students.

We don't need to create two classes.
We create one class:

class Student {
    String name;
    int age;
}
Enter fullscreen mode Exit fullscreen mode

Then create two objects:

Student s1 = new Student();
Student s2 = new Student();
Enter fullscreen mode Exit fullscreen mode

Now each object can have its own data:

s1.name = "Vicky";
s1.age = 22;

s2.name = "Prabhu";
s2.age = 23;
Enter fullscreen mode Exit fullscreen mode

Here, I have one Student class, and I created two objects, s1 and s2, using the Student class.

The Student class defines the state and behavior of Student objects. Each object, such as s1 and s2, has its own state values and can perform the behaviors defined by the class.

Real-world example

Think about a Car.

The design of a car can be represented by a class:

class Car {

    String brand;
    String color;

    void drive() {
        System.out.println(brand + " is driving");
    }
}
Enter fullscreen mode Exit fullscreen mode

Now create objects:

Car car1 = new Car();
Car car2 = new Car();
Enter fullscreen mode Exit fullscreen mode

Give them different values:

car1.brand = "Honda";
car1.color = "Black";

car2.brand = "Toyota";
car2.color = "White";
Enter fullscreen mode Exit fullscreen mode

Both objects are created from the same class, but they have different data.

Class VS Object

There are three things

Student s1 = new Student();
                
 class  reference  object
        variable
Enter fullscreen mode Exit fullscreen mode
  • Student → reference type/class
  • s1 → reference variable
  • new Student() → object created in memory The reference variable s1 refers to the object.

A class is a blueprint or user-defined data type that defines the properties and behaviors of an entity. An object is an instance of that class created at runtime.

References

https://www.geeksforgeeks.org/java/classes-objects-java/
https://www.w3schools.com/java/java_classes.asp
https://www.programiz.com/java-programming/class-objects

Top comments (0)