Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What occurs when object is created in Java?

My teacher gave me a question:

"What occurs when objects are created in Java".

To the best of my knowledge, memory allocation, variable initialization and constructor method invocation happen when an object is created.

But my teacher said that I was almost right. The 2 later things are right, except memory heap. Instead, he said the memory allocation occurs. I think that object is stored in heap, so my teacher is wrong. Do you think so?

like image 341
vuquanghoang Avatar asked Jun 12 '13 05:06

vuquanghoang


People also ask

How are object objects created in Java?

Objects in Java are created on heap memory. An object is created based on its class. A class is a template that defines states and behavior defines how an object is created. When a Java object is created following steps will happen one by one – JVM allocates 8 bytes of memory for the reference variable & assigns a default value as null.

When a Java object is created what happens to the memory?

When a Java object is created following steps will happen one by one – JVM allocates 8 bytes of memory for the reference variable & assigns a default value as null. JVM will verify whether class loading is done or not, if the class is already loaded then it will ignore or else it will perform class loading.

What happens when an object is created from a class?

But when an object is created from a class, memory is allocated to it. The object contains all the data members and methods defined in the corresponding class. In Java, everything except primitive data types is an object. In Java programming, these objects interact with each other by invoking each other’s methods.

What is a class in Java?

What is a class in Java. A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical. A class in Java can contain: Fields. Methods. Constructors.


3 Answers

As always, the best place to find a solution for these kinds of questions is in the Java Language Specification.

Specifically, from the section on new instance creation, it can be understood that this is the sequence when a new object is created, as long as no exceptions occur:

  1. Memory is allocated.
  2. Fields are initialized to their default values.
  3. The "first line" of the chosen constructor is invoked, unless it's an Object. By first line I mean either explicit call to super() or this(), or an implicit call to super().
  4. The instance initializer is executed and the fields are initialized to their requested values (actually field initialization is usually compiled as an inline part of the instance initializer).
  5. The rest of the constructor code is executed.

Now, it is possible that your teacher is talking about memory allocation as an actual operating system call - and in that case he's right in the sense that the JVM manages its own heap and thus a Java memory allocation does not necessarily translate to an OS memory allocation call (although it may).

like image 162
Oak Avatar answered Oct 14 '22 16:10

Oak


I'll answer that using a simple example.

Say you have a class Car. Now you write:

Car car;
car = new Car();

The first statement creates a reference with car in the stack.

In the second statement, the Car class will be loaded to the main memory, then it will allocate memory for the members of Car in the heap. When this happens, the members will be initialized with values provided by the JVM.

like image 36
Maroun Avatar answered Oct 14 '22 15:10

Maroun


While the JVM is running the program, whenever a new object is created, the JVM reserves as portion of the Heap for that object (where the object will be stored). The amount of Heap that gets reserved is based on the size of the object.

The JVM maps out this segment in the Heap to represent all of the attributes of the object being stored. A reference (address in Heap) to the object is kept by the JVM and stored in a table that allows the JVM to keep track of all the objects that have been allocated on the Heap. The JVM uses these references to access the objects later (when the program accesses the object).

like image 38
Jay Gajjar Avatar answered Oct 14 '22 16:10

Jay Gajjar