r/learnjava • u/NaveenCoder • Sep 22 '18
What exactly object is?
What happens internally when creating an object? What information is stored in ram about particular object? Object is nothing but a value in heap? I want to have deep insights about object.
0
Upvotes
2
Sep 22 '18
When an object is created, memory is allocated for that object.
Student student=new Student();
So here, we are creating an object of the Student class type, which has the name student.
Student student1=new Student();
Here, we are creating an object of the Student class type, with the name student1.
Memory will be allocated seperately for each object.
So, imagine memory is 100 for student. 101 is for student1 and so on.
0
2
u/8igg7e5 Sep 22 '18
This isn't specified in the language (or the platform, though features of the stored object are required such as a monitor). Each JVM can do it differently, but they generally boil down to the following...
A pointer to the class (or it's vtable or an equivalent - but always traceable back to the class)
The object monitor, used in threads synchronising against this object.
Some flags such as GC state
Space for instance fields declared in the class of the object or a super-class
You should never need to care about the exact layout - especially as the optimiser could choose to elide unreferenced content or turn a 'new Object' into space on the stack instead of a heap allocation at all (via escape analysis).