Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static variables and memory

 
Rahul Ravindran
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

For instance variable , memory is allocated when constructor is invoked . When is the memory allocated for a static variable .
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static Variables are initialized and Static Blocks are executed when class is first loaded.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abhilash Sharma
Ranch Hand
Posts: 67
Eclipse IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic