Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

where final and static variable stored?

 
vianyrajnish rajnish
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64980
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49445
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic