Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes where does static variable reside Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "where does static variable reside" Watch "where does static variable reside" New topic
Author

where does static variable reside

Imtiaz Nizami
Greenhorn

Joined: May 19, 2007
Posts: 12
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.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
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

Joined: May 19, 2007
Posts: 12
Thank you both. Can I get an answer to where does static variable live in terms of stack, heap etc. Thanks again.
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60
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 ]

The Zen of Java Programming.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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".


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.


"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Nov 18, 2006
Posts: 688


[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 ]
 
Consider Paul's rocket mass heater.
 
subject: where does static variable reside
 
Similar Threads
nx:All of URLy Bird 1.1.3 about read/write lock
Static variable
main() is the entry point
question about static variable's residence
Isn't this contradictory ?