• 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

Non static initialization

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does it mean to put some code in a block? Please look at this code. What is the difference in putting the lines in bold in a block vs not putting them in a block?

Thanks,
Shails
 
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code in a block is an instance initializer block. It is executed each time an instance of the class is created. You can have any number of instance initializers. Instance initializers are executed after superclass construction but before the constructor of the current class is executed. Otherwise, they are executed in top-down declaration order.
Contrast with the idea of a static initializer block, which is executed when a class is loaded. A static initializer block looks the same as an instance initializer block except it is preceded with the word static.
One significant use for instance initialization blocks is to allow for initialization of classes that can't have constructors--anonymous inner classes.
-Jeff-
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic