aspose file tools*
The moose likes Beginning Java and the fly likes where final and  static variable stored?  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "where final and  static variable stored?  " Watch "where final and  static variable stored?  " New topic
Author

where final and static variable stored?

vianyrajnish rajnish
Ranch Hand

Joined: Apr 22, 2007
Posts: 70
Hi all,

I want to know..
1) where the final and static variables are stored ?
2)and, also what is the scope of the final variable declared inside the method of a class.?

THANKS,
Vinay rajnish
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Please take the time to choose the correct forum for your posts. This forum is for questions on Servlets.

This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39435
    
  28
Not sure about final and static variable storage. Aren't they on the heap? Maybe somebody else will know.

And scope of a final variable inside a method . . . that method at the most.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Campbell Ritchie:
Not sure about final and static variable storage. Aren't they on the heap? Maybe somebody else will know.


It's a special part of the heap called the "permanent generation".


And scope of a final variable inside a method . . . that method at the most.


More specifically, from the declaration to the end of the block it's declared in - just like for a non-final local variable.


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
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by Ilja Preuss:
It's a special part of the heap called the "permanent generation".


Where can I read more about this ?

[I am asking for your suggestion rather than permanent generation is full because of your demonstrated skills in the JVM ]

Also, what is this leading up to:
Why does a full permanent generation trigger Full GC?

Found on my search link given above.


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Since this is posted in Beginner, I would note that there's really no reason most Java programmers would need to know or care where a variable or object is stored. If it helps your mental picture of how things work, great. But it isn't something to be worried about in general.

Some information about the permanent generation can be found here.

As to the original question: if we're talking about where the variables are stored (as opposed to object instances which are referenced by variables) then final has nothing to do with it. Instance variables are stored as part of the instances, which are stored on the heap. Class variables are stored as part of the Class object associated with that class. Instances of Class are also stored on the heap, specifically in the permanent generation. Despite its name, objects in the permanent generation are not necessarily permanent - but they are a lot less likely to get collected than other objects are.

If the intent of the question was to ask about other objects which are referenced by static final variables, I'm not certain, but it seems like there's a good chance that the JVM will decide that any object referenced by a static final should be moved to the permanent generation too. Unless you use reflection, there's no way to lose the reference to that object unless the class holding the static is collected. That's rare, but possible - and that's why I said the permanent generation isn't necessarily permanent.
[ May 13, 2007: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
First of all :

1. Trying to revive a very old thread, so not sure if anyone will read this.

2. Been a long time since i visited the Ranch, but needed to look for something and sure enough, ended up finding it here.

Now...

'Final variable declared in a method has the same scope as other non-final variables in the method, ie, as long as the method (or block in which the variable is declared) is being executed'.

When I declare an anonymous inner class in a method and need the inner class to use a variable declared in the enclosing method, I have to declare it as final. Cant pass a non-final variable.
This leads me to the conclusion that final variables are treated differently than non-final variables.
This link refers to the final variable being stored in a 'more permanent area of memory' which i guess refers to the 'permanant area'.

Is this inference correct or am I missing out on something?

Also - similar thread here
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by Neeraj Dheer:

'Final variable declared in a method has the same scope as other non-final variables in the method, ie, as long as the method (or block in which the variable is declared) is being executed'.

When I declare an anonymous inner class in a method and need the inner class to use a variable declared in the enclosing method, I have to declare it as final. Cant pass a non-final variable.
This leads me to the conclusion that final variables are treated differently than non-final variables.
This link refers to the final variable being stored in a 'more permanent area of memory' which i guess refers to the 'permanant area'.

Is this inference correct or am I missing out on something?


I think you may be over-thinking the issue. An inner class can't access a local variable because it has a different scope as the inner class. Period.

A final local variable is a special case, not because the scope of the local variable is different -- but because since it can't change, the compiler generated code for the inner class to have a copy of the variable. (At instantiation time, when both are in scope)

The compiler gave the inner class a copy of the variable. It didn't change the scope of the local variable.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: where final and static variable stored?