• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Internals  RSS feed

 
vivek dhiman
Ranch Hand
Posts: 155
Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Today I faced one question which not able to answer. We have object ClassDemo obj=new ClassDemo() . What process happens (elaborate in serial order) inside jvm when class load only specific to new ClassDemo() statement.

Can someone put some light on this.

Thanks
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just from the top of my head, making some assumptions.

The jvm first reserves some space on the heap, and zeroes it out. For every super-type of ClassDemo, starting with Object and ending with ClassDemo, it initializes fields, then runs the initializer block, and finally the constructor body.

At some point during all of this, it assigns a reference to the memory block to the obj variable. Probably at the start, after the memory is initially reserved.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if this was the first encounter of the ClassDemo class name, the jvm will first load the class itself, and initialize the static fields and run the static initializer block.
 
vivek dhiman
Ranch Hand
Posts: 155
Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

Just got the concept Classloading might be bit related : Loading --> Linking (Verifying -->Preparing --> Resolving) --> Initializing.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!