• 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

Static Initialization Block

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone answer this? In what way is a Static initialization block used?
 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Here are 2 cases when I often use static initializers.
Other programmers could probably add more scenarios.

1. When I'd like to initialize some static members, but the initial value is too complex to be calculated at a simple declaration lines.
For example:


2. When you'd like some resources to be initialized and made available before anyone uses the class. Some well-known examples:
a) JNI : when a java class has native methods, for example methods that invoke compiled C++ libraries stored in a DLL. Then your class would usually have a static initializer that loads the required DLL, so that its available for use by your java class.
b) JDBC drivers: most JDBC tutorials would tell you to start by 'loading' the jdbc driver using 'Class.forName'.
When you call 'Class.forName', you acaully invoke the driver's static initializer, which usually registers this driver with the 'DriverManager' (if i'm not mistaken) - so that, from now on, DriverManager knows about the driver and can use it for opening connections.

Hope this helps.
 
S Chet
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I get the idea. Thank you Sol.
 
I found some pretty shells, some sea glass and this lovely tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic