• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

where does static variable reside

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Local variables of a method reside on stack. Instance variables reside on the heap within objects. As one of the positive sides of declaring static or class variable is to save memory I am sure it cannot reside in every object. Where does it live?

Also, please explain if there might be a reason to declare a static variable within a method? I know using static variable in 'C' functions will maintain variable's value when the method is called subsequently.
 
author & internet detective
Posts: 42152
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Imtiaz Nizami:
Also, please explain if there might be a reason to declare a static variable within a method? I know using static variable in 'C' functions will maintain variable's value when the method is called subsequently.


Java does not have the concept of a static variable within a method.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that static variables "live" in a fixed location in RAM - whether or not that means the stack, I'm not too sure. The stack "lives" in RAM but is generally supported by the processor via a stack pointer.

Regards,
JD
 
Imtiaz Nizami
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both. Can I get an answer to where does static variable live in terms of stack, heap etc. Thanks again.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure, but the Class instance for the class, could be a good
place for the static variable. The Class instances are in the
method area, which most of the time is part of the heap.
[ June 02, 2007: Message edited by: Pravin Jain ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know, static variables reside in the same space as the class' byte code - a special area of the heap called the "permanent generation".
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wouldn't you want to separate static variables from byte code so that the byte-code could be read-only and the static variables could be read-write?

Kaydell
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Ilja]: As far as I know, static variables reside in the same space as the class' byte code - a special area of the heap called the "permanent generation".

That's my understanding too. I think both are inside the Class object associated with the class, which is stored on the heap, in the permanent generation.

[Kaydell]: Wouldn't you want to separate static variables from byte code so that the byte-code could be read-only and the static variables could be read-write?

I don't believe that's necessary. The JVM places sufficient restrictions on code so that you can't accidentally or intentionally write to areas of memory you're not supposed to.* Even if it's right next to something you are supposed to be able to write to.

*One might quibble over what "supposed to" means here, since reflection allows various ways to circumvent some of the rules of Java. But you still can't overwrite the code portions of a class definition.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kaydell Leavitt:
Wouldn't you want to separate static variables from byte code so that the byte-code could be read-only and the static variables could be read-write?



How would separating them in memory help with that?
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


[Me]: Wouldn't you want to separate static variables from byte code so that the byte-code could be read-only and the static variables could be read-write?



My idea is apparently wrong. I agree that In Java, we don't need to worry about unititialized pointer since all object references aren't pointers and object references are either initialized to null or you get a compile-time warning. Also, we don't have pointer arithmetic. Also, in Java, you can't index off the end of an array and stomp on something that you shoudln't since we are protected from ourselveswith the ArrayOutOfBounds exception and we are protected by the NullPointerException.

I accept what people have been telling me that there is not need for memory protection for where the byte-code and the static variables live (wherever that may be).

Kaydell
[ June 03, 2007: Message edited by: Kaydell Leavitt ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic