• 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

When to use Static initial block?

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

Can we use constructor to initialize static members of a class.
If this is possible then what is the use of static initial block.

Can anyone explain me in which scenario we go for static initial block??

Thanks in advance..
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rakesh Ss wrote:... Can we use constructor to initialize static members of a class.
If this is possible then what is the use of static initial block...


A constructor is only called when an instance is being created. So for static (class) members to be properly initialized, a constructor will not work, and a static block might be required instead.
 
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Except if the static fields are only used through instance methods, then you can do lazy initializing through the constructor.
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're asking about a "static initialization block," right?
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static blocks -are static ,they run when class is loaded and not associated with any instances /objects of class or constructor(constructor constructs an object)


Stephen -

"Except if the static fields are only used through instance methods, then you can do lazy initializing through the constructor."

I am really interested in the exceptional case that you mentioned.Can you please provide some examples to explain better.

Thanks.
 
Stephan van Hulst
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here is such an example. Let's say Expensive objects are very expensive to create. You don't want to create them until you really have to. If the Expensive object is only used by instance methods, like plus(), you can avoid creating one until the first instance of Test is created. This way, if a program only needs the help() method, it can avoid creating a Test (and thus an Expensive) altogether.

This is a rare situation, but it's just an example.
reply
    Bookmark Topic Watch Topic
  • New Topic