Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Final object can be modified but reference variable cannot be changed

Tags:

java

A reference variable marked final cant reassigned to different object.The data with in object can be modified but the reference variable cannot be changed.

Based on my Understanding I have a created a code below where I am trying to reassign a new UserId of 155.As the Definition goes I am only trying to change data within the object. But the reference is same.

public class FinalClass 
{   
    public static void main(String[] args)
    {
        ChildClass objChildClass = new ChildClass(); 
        objChildClass.UserId = 155;
    }
}

class ChildClass
{
    public static final int  UserId = 145;  
}

I believe I misunderstood the above concept.

Kindly explain the same with example.

Thanks for Reply.

like image 475
Java Beginner Avatar asked Feb 21 '13 08:02

Java Beginner


2 Answers

You can't change final value using "=" operator. If you do it, you try to change the reference (or primitive) and final states that this cannot be changed.

You can change existing object's fields:

public static final User user = NewUser(145);

    public static void main(String[] args)
    {
        user.setId(155);
    }
like image 118
BobTheBuilder Avatar answered Sep 21 '22 00:09

BobTheBuilder


Your understanding of the concept was right. Wait I will try to explain the beauty of final keyword. i have divided it in three parts :

  1. If you are using final keyword for any member(local variable/instance variable/method) it means there is no way that you can modify the value of that particular variable(if method, that can not be over-ridden) throughout your program.
  2. If you are declaring a class as final, then, it means that no other class(in same or different package) can extend that class(final class), in other words the final class can never be subclassed, but, the final class can be used as the "Superclass reference".
  3. Third is the case which you are referring to. If any object reference variable is declared as final, then it means that the final reference variable can never ever in its entire life refer to a different object but the data within the object(the object your reference variable is referring to) can be modified.

I have written a class here hope that clarifies all the doubts you have.

public class FinalSampleTestDrive {

public static void main(String[] args) {

    final FinalSample obj = new FinalSample();

    FinalSample obj2 = new FinalSample();
    FinalSample obj3 = new FinalSample();

    obj2.setName("arya");
    System.out.println(obj2.getName());

    obj3 = obj2;  //allowed

    System.out.println(obj3.getName());

    //obj = obj2 //not allowed as obj is final and can not be modified

    obj.setName("shubham");
    System.out.println(obj.getName());

    //but the value of the instance variables, the obj is referring to
    //can change

    obj.setName("shivam");
    System.out.println(obj.getName());



}

}

and this is the FinalSample class which is getting instantiated here :

public class FinalSample {

private String name;
private String age;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAge() {
    return age;
}
public void setAge(String age) {
    this.age = age;
}

}

You try to run it in different ways on your machine.

Happy Coding :)

like image 42
Shubham Arya Avatar answered Sep 23 '22 00:09

Shubham Arya