• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Instance Block & Constructor

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having doubt about the invocation of Instance Block.
I have read like , "Instance block will be executed soon after the call to super statement from the constructor".
I have also read that " The Java compiler copies initializer blocks into every constructor."
So can anyone please help to understand how exactly is the instance block invoked and from where ?

Thank You.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The instance initializers are executed after any superclass constructors have completed and before any of the code that you put into any current class constructor is executed.

The way this is implemented (in at least some JVMs, but maybe not all) is that the compiler just copies that code into the start of each constructor (or rather, each constructor that doesn't invoke another current class constructor with this(...) as its first statement).

I think that this is the way it has to happen, because I don't think there's any such thing as an instance initializer in the bytecode. However, it's possible that this isn't actually required, and it just happens to be how it's implemented in some JVMs. You can look it up in the JVM spec and JLS yourself if you're that curious.

Regardless of that particular detail, however, the spec says when they have to execute, and the compiler and JVM are written to make sure that's what happens.
 
Marshal
Posts: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is actually strictly defined. It’s all in the Java Language Specification.
 
Veena Nair
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Both for the valuable information.
I went through the jls , but still couldn't make out from where it get's invoked.
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic