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?
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 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.
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. 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.
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:
Object
. By first line I mean either explicit call to super()
or this()
, or an implicit call to super()
.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).
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With