| Author |
Static variables and memory
|
Rahul Ravindran
Greenhorn
Joined: Sep 03, 2012
Posts: 3
|
|
For instance variable , memory is allocated when constructor is invoked . When is the memory allocated for a static variable .
|
 |
R. Jain
Ranch Hand
Joined: Aug 11, 2012
Posts: 276
|
|
Rahul Ravindran wrote:
For instance variable , memory is allocated when constructor is invoked . When is the memory allocated for a static variable .
Because, static variables are class variables, they have to be available and initialized even before any instance of class is created.. You can guess when..
|
OCPJP
|
 |
Abhilash Sharma
Ranch Hand
Joined: Dec 05, 2010
Posts: 67
|
|
|
Static Variables are initialized and Static Blocks are executed when class is first loaded.
|
OCPJP 6 | OCEJWCD 6
|
 |
gurpeet singh
Ranch Hand
Joined: Apr 04, 2012
Posts: 869
|
|
Anishashu Sharma wrote:Static Variables are initialized and Static Blocks are executed when class is first loaded.
i don't think they are initialized at class loading time. they are initialized during class initialization which occurs after class loading. according to JLS class initialization involves inititalization of class variables and static initializer (static init blocks). class initialization occurs just before the first occurence of following: Say T is a class and
1. instance of T is created
2. static method of T is invoked
3. static variable of T is accessed
4. static variable of T is assigned and it is NOT a compile time constant.
|
OCPJP 6(100 %) OCEWCD 6(91 %)
|
 |
Abhilash Sharma
Ranch Hand
Joined: Dec 05, 2010
Posts: 67
|
|
gurpeet singh wrote:
Anishashu Sharma wrote:Static Variables are initialized and Static Blocks are executed when class is first loaded.
i don't think they are initialized at class loading time. they are initialized during class initialization which occurs after class loading. according to JLS class initialization involves inititalization of class variables and static initializer (static init blocks). class initialization occurs just before the first occurence of following: Say T is a class and
1. instance of T is created
2. static method of T is invoked
3. static variable of T is accessed
4. static variable of T is assigned and it is NOT a compile time constant.
Generally, "class loading" refers to loading, and initializing the class definitions.... meaning loading the bytecodes from the class file, creating the Class class, running the static initializers, etc.
Once a class is loaded, and initialized, then it would be possible to instantiate an instance of the class -- ie. create an object of that class type.
|
 |
Javin Paul
Ranch Hand
Joined: Oct 15, 2010
Posts: 276
|
|
|
On related note, you can utilize this feature of static variable to create Thread-safe Singleton or for anything which requires thread-safe initialization but remember this is not lazy loading i.e. you may your object ready to use even before some one actually requested for it.
|
http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
|
 |
 |
|
|
subject: Static variables and memory
|
|
|