DEV Community

Cover image for OOPS - JAVA (Part A)
Abhijeet Vishwakarma
Abhijeet Vishwakarma

Posted on • Edited on

OOPS - JAVA (Part A)

Classes and Objects

Understanding Classes and Objects is actually quite easy.

Suppose you are a student, and your teacher asks you to write a datatype that can store the names of 5 students.
You would say, “Okay,” and write:

String[] names = new String[5];
Enter fullscreen mode Exit fullscreen mode

Now your teacher asks you to write a datatype to store 5 roll numbers.
You say, “Okay,” again:

int[] roll = new int[5];
Enter fullscreen mode Exit fullscreen mode

Then she says, “Write a datatype to store the names, roll numbers, and marks of 5 students.”
You think, “That’s still easy,” and write:

String[] names = new String[5];
int[] roll = new int[5];
float[] marks = new float[5];
Enter fullscreen mode Exit fullscreen mode

But now she says, “This is fine, but I want one single datatype that can store all three things (names, roll numbers, and marks) together.”

Now you get confused—because you've learned that an array of a particular type can only store values of that type.

An int array can store only integer values.

A String array can store only string values.


This is where Classes and Objects come into the picture.

Just imagine how useful it would be if you could create your own custom datatype.
Classes allow you to do exactly that.

Let’s look at an example:

class Student {
    String name;
    int roll;
    float marks;
}
Enter fullscreen mode Exit fullscreen mode

This is what you should have written when your teacher asked for a single datatype to store a student’s name, roll number, and marks.


Understanding What’s Happening

In the above example, we've created a class named Student, which has three instance variables: name, roll, and marks.
You can think of the class Student as a custom datatype that can store a mix of int, String, and float values together.

Definition of a class:
A class is a named group of properties (variables) and functions (methods).

The variables inside a class are known as instance variables, which represent the properties of that class.
(We’ll learn about functions/methods later.)


Trying to Understand Classes Even Better

Suppose you are the owner of a mobile phone manufacturing company.

Naturally, you’ll have a blueprint or a template for your phones — they will have a model_name, RAM, storage, and price.

A class is that blueprint or template. For example:

class Mobile {
    String model_name;
    int ram;
    int storage;
    float price;
}

Enter fullscreen mode Exit fullscreen mode

Now, using this template, you can create as many mobile phones (objects) as you want.


What is an Object?

An object is simply an instance of a class.

Let’s say you want to create a new phone model with 12GB RAM, 512GB storage, and a price of ₹20,000.
You can create it like this:

Mobile one = new Mobile();
Enter fullscreen mode Exit fullscreen mode

Here, one is a reference variable that refers to an object of the Mobile class.

Now assign values to its properties:

one.model_name = "ABC";
one.ram = 12;
one.storage = 512;
one.price = 20000f;

Enter fullscreen mode Exit fullscreen mode

Complete Example

// Define the class (your custom datatype)
class Mobile {
    String model_name;
    int ram;
    int storage;
    float price;
}

// Main class to run the code
public class Main {
    public static void main(String[] args) {
        // Creating an object of the Mobile class
        Mobile one = new Mobile();

        // Assigning values to the object's instance variables
        one.model_name = "ABC";
        one.ram = 12;
        one.storage = 512;
        one.price = 20000f;

        // Printing the mobile details
        System.out.println("Model Name: " + one.model_name);
        System.out.println("RAM: " + one.ram + "GB");
        System.out.println("Storage: " + one.storage + "GB");
        System.out.println("Price: ₹" + one.price);
    }
}

Enter fullscreen mode Exit fullscreen mode

Conclusion : Class is logical, Object is a physical reality which actually occupies space in the memory.

I've been diving into Java OOP concepts, and today I finally understood Classes and Objects clearly — all thanks to Kunal Kushwaha’s amazing content. 🚀

Here’s how I understood it:
➡️ Arrays can store one type — names in String[], roll numbers in int[], marks in float[].
➡️ But when I wanted to store all three together for each student — I realized I needed something more powerful.
➡️ That’s when Classes made sense — like creating my own datatype that holds all related info together.
➡️ And then comes the Object, which is like a real version of that datatype — just like a phone made from a blueprint.

Honestly, the concept feels so much simpler now.

📚 Sharing this as part of my learning journey — and maybe someone else finds it helpful too.
Would love to connect with fellow learners. Let’s grow together!


Top comments (0)