• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Where do static variables go?stack/heap

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are static variables stored? heap or stack?
(Was that a foolish question?? )
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A static varaible can never be a local varibale so it goes and lives on heap.

(Was that a foolish question?? )


surely not.
 
Benjamin Samuel
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess Sanjeev Kumar meant stack!
static variables go on stack.
member variables are heap variables.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Sunil,

i bet he ment heap.

On which threads stack do you think it would make sense to put them?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

According to my understanding, static variables store in stack and not in Heap as only instance variables/objects live in Heap.(static variables doesnt belong to instance its class variable).
If anyone has detailed answer to it.Please post.

Harish
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi let us start with the basics.

Consider the Main method.
It is static right...And we observe that it is on Stack.

Hence i think whatever comes as static geos to Stack.....

Is this information helpful..
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a class A is loaded an object is created representing the class. This object is an instance of the class Class. From this object you can obtain other objects representing the variables (both static fields and member fields) of the class A. All these objects are held in the heap. The only variables held on the stack are local variables of a method.

You can read the Java API for class java.lang.Class and read the Java Virtual Machine Specification for detailed information.
[ January 14, 2007: Message edited by: Barry Gaunt ]
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic