• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Static variables and memory

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For instance variable , memory is allocated when constructor is invoked . When is the memory allocated for a static variable .
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 67
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static Variables are initialized and Static Blocks are executed when class is first loaded.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Would you like to try a free sample? Today we are featuring tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic