wood burning stoves 2.0*
The moose likes Java in General and the fly likes static intializers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "static intializers" Watch "static intializers" New topic
Author

static intializers

Bhayajis Bhayajis
Greenhorn

Joined: Sep 29, 2003
Posts: 9
1.What are static initializers.
2.how is static block different from static method.
3.where is a static block created - heap or satck
4.does the static variable stay even after the method using it dies (as in case of final variables which are created on the heap) soumyaharish@rediffmail.com
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
1.What are static initializers.

It is a block of code that is executed only once, when the class is loaded.
For example,

In the above code the static block will be executed when the class loaded thus initializing the variable staticData
2.how is static block different from static method.

Static blocks are not methods actually. Unlike a method, the static blocks doesn't have a name, aruguments, access modifiers and return value. These blocks can never be called in a program. The only time this block is executed is when the class is loaded.
3.where is a static block created - heap or satck

4.does the static variable stay even after the method using it dies (as in case of final variables which are created on the heap)

I can answer these questions, but it will certainly confuse you more!

Any experts out there (Cindy / Jim / ....)


Mani
Quaerendo Invenietis
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34


3.where is a static block created - heap or satck

They're not created anywhere -- they're code, not data. They live, like all Java code, entirely outside either the heap or stack.

Saying that "final variables are created on the heap" is misleading. Final variables exist, again, in that mysterious native memory area which is outside either the Java stack or the Java heap.
In any event, the lifetime of a static variable is as long as the class it belongs to is loaded. If the class is unloaded, the static variable goes away.


[Jess in Action][AskingGoodQuestions]
 
Don't get me started about those stupid light bulbs.
 
subject: static intializers
 
Similar Threads
Static method
Variables created in Static method
GC behavior towards static objects
Static block and method.
Static is painful!