Solved

Memory Allocation for Final variables

Posted on 2002-06-02
6
857 Views
Last Modified: 2012-05-05
How is the memory allocation for final variables done ? Does each  instance of the class in which the final variable is declared will have a copy of final variable or there is only one copy of final variable existing accross all the instances like static variables?
0
Comment
Question by:send2sachin
6 Comments
 
LVL 7

Expert Comment

by:yoren
ID: 7050447
I haven't seen the specific JVM behavior for this explained, but unless it's really stupid Java will keep only one copy across all instances. Final variables are constants and can never be modified at run time.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7050483
The JVM will allocate memory for all the final members in each object. What you gain by declaring a member as final is that the JVM can optimize the utilization of such kind of variables (... in this case - constants...)
0
 
LVL 7

Expert Comment

by:yoren
ID: 7050489
Venci, how do you know? I'd love to read about this if you can point me to a site.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:yoren
ID: 7050676
Nevermind. I was thinking of native types, which can be optimized. If you've got an object type then you will of course have to allocate the space for the object reference, plus any actual objects you create.
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 7051337
When a variable/field is modified by 'static' then it is a class variable which exists once for the class. When a variable/field is not modified by 'static' then it is an instance variable which exists once for each instance of the class. This holds true regardless of the presence of other modifiers like 'final'.

The use of the 'final' modifier for a variable has application beyond the scope of simply declaring constants of a class. This is by far the most common use of final because it allows a class developer to provide a public field which can be initialized to a specified value and retrieved without nuisance of an accessor method but cannot be changed by clients of the class.

In the more abstract, a final variable is one which may only be assigned to once. That does not restrict finals from being either class variables or instance variables. Just because a the value may be set only once does not mean that it must be the same for every instance of a class. The language permits for a 'blank final' in which a final variable is not given a value by a declaration initializer expression. Instead, initialization occurs through an instance initializer or by the class constructors. Instance initializers are useful when instance variables (including blank finals) must be initialized by code which must catch exceptions, or perform other kinds of control flow which cannot be expressed in a single initializer expression. In this scenario, instance variables can be declared final so they may be only be set once for the instance but the specific values may be different for each instance. As an example, the Integer class could implement immutability by declaring its internal value as final (NOTE: it isn't actually implemented this way):

public class Integer {
    public final int value;

    public Integer(int value) {
        this.value = value;
    }
}

In this (trivial) example, the value field is public so it may be directly accessed without need of an accessor method. Being final, its value can only be set once which is done in the constructor. Each instance of the Integer class thus has its own final variable with an instance-specific value determined by construction.  

Another common use of finals is with method parameters, catch parameters, and auto-locals (method variables) so that they can be referenced by anonymous inner classes. In fact, if a local variable or parameter in one class is referred to by another (inner) class, it must be declared final. Because of potential synchronization problems, there is by design no way for two objects to share access to a changeable local variable.

So the 'final' modifier has application beyond simple declaration of constants, although that is the primary use you will find of it in production code. It is not an indicator of whether a declared field will occur once for the class or once in each instance of the class. Use the 'static' modifier to make that determination.

Best regards,
Jim Cakalic
0
 

Author Comment

by:send2sachin
ID: 7055463
Thanks Jim for such a convincing answer.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
print map entry 34 54
more than one jdk and one jre 1 30
expectj telnet failing 5 19
MySqlDump not dumping triggers 1 13
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now