Solved

Memory Allocation for Final variables

Posted on 2002-06-02
6
848 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
Comment Utility
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
Comment Utility
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
Comment Utility
Venci, how do you know? I'd love to read about this if you can point me to a site.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 7

Expert Comment

by:yoren
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks Jim for such a convincing answer.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

763 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

6 Experts available now in Live!

Get 1:1 Help Now