What happens when you create an OBJECT in java?

Ravi Chandola
Javarevisited
Published in
3 min readNov 19, 2022

To understand object creation, we should first know the concept of OOP. It's a programming model which stands on the four core pillars of object-oriented principle principles i.e, Abstraction, Encapsulation, Inheritance, and Polymorphism. We will later discuss these four pillars in our upcoming blog but first, we need to understand the term object.

An object is nothing but an instance of the class. It has state, behavior, and identity. The state and the behavior of an object can be defined in the class.

class Medium{

String articles; // state 1
int pageNumber; //state 2

public void writeArticle(){ // Behaviour 1
// some code ....
}

public void publishArticle(){ // Behaviour 2
// some code .....
}
}

State” is also known as the data member or fields or properties of the class whereas “Behaviour” is the method or member function of the class.

In short, we can say that Class = State + Behaviour. It's an expanded concept of structure that we read in our Procedure oriented Programming such as C, C++, etc. By the above definition, we can easily say that a class is a blueprint of an object’s state and behavior.

That's so much about class but to understand object creation, one should understand the concept of class. Now come to the point where we are going to understand the concept of object creation but before going to that we should know what object means.

An object is a physical representation of the class, which is a runtime entity as it allocates memory in the heap while executing the class. In order to create an object of class we should know the syntax for the same.

Syntax : <ClassName> <referenceVariable> = new <ClassName> ();

Medium med = new Medium();

If you understand till here, then it means we are going on the right track because now it's time for the climax ……

So When I am creating an object of class i.e,

Medium med = new Medium();

JVM allocates 8 bytes of memory for the reference variable and initializes the same with the value null.

Verifies whether the class is loaded or not. If in case the class is not loaded then first it will loads while loading it allocates memory for the static variable which is not there in our class but if it was there then it will initialize that variable with a default value i.e, for int -> 0, String -> null and float -> 0.0

After that, it will create an object and allocate the memory for an instance variable of the class and initialize it with the default values.

Now the address of the object is assigned to the reference variable.

Object assigned to the reference variable

I hope you have understood this article and have a clear picture of object creation via diagrams. If you like my content, do clap and follow. Thank you !

--

--

Ravi Chandola
Javarevisited

As a technology enthusiast, I have a passion for learning and sharing my knowledge with others. https://www.linkedin.com/in/ravi-chandola-304522133