Objects in Java

Praboda karunarathna
2 min readMay 11, 2022

--

Difference between class and object

Object is a real world entity such as table, pen, or laptop. It is an instance of a class. Class is a template of creating objects. It is a group of similar objects.

Each object has state and behavior. Instance variables and methods represent the state and behavior of the object respectively.

Let’s take “Car” as an example.

Each car has brand,model number,tyres,color. Those are states of the “Car” object. Start,stop,drive are the behaviours of this object. So we can design the Car class as below.

Class Car{

String brand,model_number, colour;

int tyres;

void start(){

//statements;

}

void stop(){

//statements;

}

}

Objects of Car class

We can use the separate test class to create multiple objects and run the methods. Each object created in Java goes into an memory area called as “heap”.

Class TestClass{

Public static void main(String args[]){

Car c= new Car();

c.brand = “abc”;

c.model_number=” x123”;

c.year=2021;

c.start();

Car c2= new Car();

c2.brand = “xyz”;

c2.model_number=”45sa”;

c2.year=2021;

c2.start();

}

--

--