Solved

Memory Allocation for Final variables

Posted on 2002-06-02
6
875 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 17 51
How do I remove an object from a 3 40
How  can  i  resolve  HTTP Status 404 -? 8 41
Notify sent to other threads in Java 9 33
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

808 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