File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static variables and memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static variables and memory" Watch "Static variables and memory" New topic
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: 367
    
    1

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..
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: 923
    
    1

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 %) OCPJBCD(93%)
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: 281

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static variables and memory
 
Similar Threads
static block
about static variable
static variables memory
strings
where do static variables live ?