Java Reference
In-Depth Information
final Reference Variables
Any type of variable (primitive and reference) can be declared final . The primary meaning of the final keyword is
the same in both cases. That is, the value stored in a final variable cannot be changed once it has been set. I will look
at the final reference variable in a little more detail in this section. A reference variable stores the reference of an
object. A final reference variable means that once it references an object (or null ), it cannot be modified to reference
another object. Consider the following statement:
final Account act = new Account();
Here, act is a final reference variable of the Account type. It is initialized at the time of its declaration. At this
time, act is referencing an object in memory.
Now, you cannot make the act variable to reference another object in memory. The following statement
generates a compilation time error:
act = new Account(); // A compile-time error. Cannot change act
A common misconception arises in this case. Mistakenly, programmers believe that the Account object that is
referenced by the act reference variable cannot be changed. The declaration statement of the act reference variable
as final has two things.
act is a reference variable, which is final ,
Account object in memory whose reference is stored in the act variable.
It is the act reference variable that cannot be changed, not the Account object it is referencing. If the Account
class allows you to change the state of its object, you can change the state using the act variable. The following are
valid statements, which modify the balance instance variable of the Account object:
There is an
act.deposit(2001.00); // Modifies state of the Account object
act.debit(2.00); // Modifies state of the Account object
If you do not want an object of a class to be modified after it is created, you need to include that logic in the class
design. The class should not let any of its instance variables be modified after the object is created. Such objects are
called immutable objects.
Compile-time vs. Runtime final Variables
You use final variables to define constants. This is the reason that final variables are also called constants.
If the value of a final variable can be computed by the compiler at compile-time, such a variable is a
compile-time constant. If the value of a final variable cannot be computed by the compiler, it is a runtime final
variable. The values of all blank final variables are not known until runtime. References are not computed until
runtime. Therefore, all blank final variables and final reference variables are runtime constants.
Java performs an optimization when you use compile-time constants in an expression. It replaces the use of the
compile-time constant with the actual value of the constant. Suppose you have a Constants class as shown below.
It declares a MULTIPLIER static final variable.
public class Constants {
public static final int MULTIPLIER = 12;
}
 
Search WWH ::




Custom Search