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

What are static initializer blocks?

 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Can you tell me what static initialization blocks do in java?
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
static block generally use to initialize the value of some variables which are required before initializing others.

Example: Need to initialize a connection of some database before you are using anything else.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Arun Kumarr wrote:Can you tell me what static initialization blocks do in java?



Static initializers contain code that get executed when a class is loaded.

Henry
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Apologies Henry and Tushar. I wanted to ask another User who posted a trivia homework on something like how to initialize spring before main method is running.
This was actually an reply to his post. Wondering my reply got as a new post. Please close this thread.

BTW, Couldn't help notice. Are the 42 in cows, not going to change?
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I moved your post because your question appeared completely unrelated to the other user's post. New and unrelated questions are often moved to their own thread to not confuse the existing thread.

Remember that this is a public forum, where answers are encouraged from all users. To dismiss Henry and Tushar's input like this is poor form. You should embrace all input.
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Ah!!. I see the confusion. My original post told him directly what static initialization does.
Later I edited it to ask him back a question, thinking the OP would go search for the answer and in the process learn what static initialization does and probably by trying to explain it, learn it deeper.
I never wanted to give him a direct answer and spoil the learning.

... and nope. Been in JR for long. I wanted Henry to close it, so that no one elses' time is wasted trying to answer my question. Just in good spirits Tim.


Oh!! the 42 cows joke was related to the ultimate answer. Nothing offending. I've been misunderstood couple of times in JR. May be English is not my first language.

Cheers.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Just budding in with a tangential discussion to increase the confusion ;)

Tushar Goel wrote:Example: Need to initialize a connection of some database before you are using anything else.



Tushar, it's probably never a good idea to store a connection of any kind into a static variable. Connections should only live for as long as you need them, then be disposed.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Locked as requested.

Henry
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic