[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Memory Allocation for Final variables

Posted on 2002-06-02
6
Medium Priority
?
965 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

649 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