Solved

Memory Allocation for Final variables

Posted on 2002-06-02
6
914 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

724 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