Bookmark Topic Watch Topic
  • New Topic

What are static initializer blocks?  RSS feed

 
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
Can you tell me what static initialization blocks do in java?
 
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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?
 
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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: 7993
143
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator

Locked as requested.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!