Object in Java

By | July 26, 2020

Anything which is physically present is considered to be an Object. In other words , the object is the Real world physical entity.

Before moving forward , Let me ask you a very simple question, Where are you reading this article , probably either in cell phone or in Laptop.

 For now i assuming that you might be using cell phone or mobile phone. 

The mobile phone which you are holding right now  is also considered to be an Object. 

The reason is very simple , since it is physically present or having physical existance.

Object has two things, state and behaviour. 

What is State?

State is a property or data which describe any object . For example, mobile phones have following states :-

  • Colour
  • Model Number
  • Brand Name
  • Price and many more 

All above data is describing the mobile phones, by simply looking into the Brand name you will get to know founder company of that phone.

From price you will get to know that whether this phone is under your budget or not.

In Java, State is nothing but a variables or data members present inside the class.

 Suppose you got a task to Create an Mobile phone Object in Java, so the very first thing required is Class with name MobilePhone. 

And that class must have all data members as mentioned above. 

What is Behaviour ?

Behaviour is an action or work performed by an Object. For example , mobile phones have following behaviours:-

  • Make a call
  • Send an SMS
  • Play music and many more

All above actions you can performed on your mobile phone.

In Java, Behaviour is nothing but a methods or functions present inside the class. Inside the class methods is define as shown below.

I hope you got some idea about an Object. 

Important points of an Object

  • An object is real world physical entity.
  • Anything which is physically present is called or can be considered as an Object.
  • Object has state and behaviour.
  • Object is also called instance or entity.
  • Object always created in Heap Memory.
  • JVM is responsible for creation of an Object.

To create any object in Java we need Class. Class is nothing but a design or structure using which an object get created.

 In Technically terms we define class as an blueprint or mastercopy using which we can create an Object. 

Now the very important Question , How any Object got created in Java?
Already we know class is the first requirement for creating any object. The reason is very simple , i will explain you in details , please continue reading .

Once you created a Class , you are going to write a simple line of code which actually responsible for creating any object in Java. 

For now i’m taking a referance of same MobilePohone Object.

MobilePhone mob = new MobilePhone();   // this line of code only responsible for creating any Object.

Explanation:-

Once that line of code got executed , JVM will understand that, it has to create one object of Type MobilePhone, if you notice in above line of code right side of assignment operator  there is “new MobilePhone()”,

By looking this JVM will bind or call the default constructor of MobilePhone, and we already know that default constructor is define inside class.

 So By defaut JVM will search that class. That’s why class is very important to create any object.

Hence from above it has been also cleared that , JVM is responsible for creating any Object in Java.

But there is one more thing in above line of code i.e, “mob” .What is that??

mob is simply a variable which is used to stored the address of the object which is created by JVM

Since it stored the address of  Object , so it is also called Object Reference.  It stored the address in the form of hashcode.

Relationship between Class and Object

  • A class is a mastercopy or blueprint using which we can create multiple Objects.
  • Without class we cannot create any Object.
  • Object is a real world physical entity which is an instance of a class.
  • Multiple Object can be created using single class which is called identical or singular Object.
  • When one Object is destroyed or Modified then it does not affect another object and nor to the Class.

Leave a Reply

Your email address will not be published. Required fields are marked *