• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about isolated block of code

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In this case what are these isolated blocks of code in line 5 and line 6 are called, and what purpose do they serve? I know the static block in line 2 is called when class is created in JVM, but I have no idea about the isolated blocks. When answering, can you please describe what would happen if exception is encountered in these blocks.

Thank you very much


Thank You Abhay. It was a great explanation.
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to Javaranch !!
Isolated blocks are basically constructor blocks. These constructor blocks are called when an object of this class is instantiated in JVM [ like new Confusion()] .
Question can be why do we have these constructors block available in Java when we already have well defined semantics for Constructors. Answer is these constructors blocks are there to place java code which is common to all constructors. The Java compiler copies constructor blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors. For example - if a class has 2 valid constructors and we have a code which is common to all these 2 constructors, then, instead of copying that code in 2 constructors, we can place it in common constructor level block. To put these words in code, consider below mentioned class...



Here, when we instantiate this class object, constructor block code is copied into each constructor.

In your class, since you have not provided any constructor, JVM provides you default constructor and then places two constructor blocks within it. That's why when you call this class, these contructor blocks also get executed.
Having explained this bit, I should be easy for you to give a try by generating an exception in constructor and see how that code behaves when you execute it. For example, execute below code and check what is the output.

 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add on .. follow this link to know about exception scenario - http://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html#jls-11.2.3

I liked the class name "Confusion"
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic