final variables in java

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Hello all, We all know that the value of final variables cannot be changed in java. Consider following code

We are changing the value of someNumber here so obviously compilation is not successful as it is  final  variable and cannot be changed. But what happens if we make a reference variable to an object as final ? can we change its value? Let us see.

Consider Student class

Now consider Test class as follows

Well this compiles fine and even gives the output as follows.

How is this possible? We’ve made the student variable as final, then how is it allowing to change the value?

Well, the actual meaning of final keyword is that you cannot assign a new value to the variable once you have assigned a value. Let us compare with previous example.

here we are trying to assign 10 even after assigning value 0 to someNumber previously. Thats why it does not compile.

 

In our second example,

  •  We created a Student object which is referred by reference variable student
  •  We set the name for Student object as “ prasad “.
  •  Then printed the name of Student
  •  Again set the name for Student as “ pranil “.

Here you will notice that we have just changed the contents of the Student object and we have not changed the value of student reference variable. i.e. we are not assigning a new Student object to the variable. This does not violate the final variable condition.

If we do,

then the result is as expected, it does not compile because we are assigning a the reference variable to a new Object which is not allowed for final variable.

 

 

 

 

 

Share Button

11 comments for “final variables in java

  1. Sudhir Kumar
    November 22, 2013 at 9:48 pm

    yes, what is the use to make object reference final / in real world.

    thank you

    • Prasad Kharkar
      November 23, 2013 at 12:50 am

      You may create a class and it has a reference value of which value you have defined. You have kept the class open for other developers for use but you don’t want the value of that variable to be changed by any other developer. So you can make that reference variable as final and it will not be modified by anyone else.

      • Sudhir Kumar
        November 26, 2013 at 3:26 am

        it means no one can change the value of that particular object.. like- student

        • Prasad Kharkar
          November 26, 2013 at 10:17 am

          What it means is that when you do Student student = new Student();, then you cannot assign other Student object but you can change the fields of Student object.e.g. student.setName("Sudhir");

          • Sudhir Kumar
            November 27, 2013 at 12:34 am

            oh,yes

  2. Sudhir Kumar
    November 26, 2013 at 1:55 am

    OOOO, thanks

  3. Sudhir Kumar
    November 26, 2013 at 3:11 am

    My interview Question:- Difference between
    1.) Abstract Class & Interface 2.) Constructor & Methods. I tried to give answer, He told I don’t want theory. so said some real world example .

    What will be the exact answer/real world answer.

    • Prasad Kharkar
      November 26, 2013 at 10:16 am

      I will write an article about this soon Sudhir.

  4. June 8, 2016 at 10:03 pm

    In case the instance name is not private, Does it allowed?
    final Student std = new Student();
    std.name=”hdfhdkjf”;
    System.out.println(“name = ” + std.name);

    std.name=”nasdfh”;
    System.out.println(“name = ” + std.name);
    }

    • June 9, 2016 at 10:38 am

      It doesn’t matter whether it is private or not. Here you are changing properties of object referred by std. So this is allowed. But after final Student std = new Student(); i.e. when std is referring to an object, then another assignment std = new Student(); is not allowed and it will give compiler error.

Leave a Reply

Your email address will not be published. Required fields are marked *