4

This can't be achieved, you cannot instantiate an already instantiated final object:

private void myMethod(){
    final Object object = null;

    object = new Object();
}

Eclipse gives you an error message:

The final local variable object cannot be assigned. It must be blank and not using a compound assignment

However, if you pass this object to another method:

private void _myMethod(Object object){
    object = new Object();
}

This can be achieved!

final form:

private void myMethod(){
    final Object object = null;

    _myMethod(object);
}

private void _myMethod(Object object){
    object = new Object();
}

Could anyone explain me this? It is so confusing how Java works.

Zbarcea Christian
  • 9,367
  • 22
  • 84
  • 137
  • Java do pass by value and create the shallow copy of the object being passed – Ashish May 17 '14 at 06:51
  • It's the variable thats final, the variable in your method is shadowing the instance variable (aka has the same name) but isn't the same variable – Richard Tingle May 17 '14 at 08:34

3 Answers3

4

First of all, names of your variables make it difficult to explain, so:

private void myMethod(){
    final Object finalObject = null;

    _myMethod(finalObject);
}

private void _myMethod(Object methodObject){
    methodObject = new Object();
}

Java passes objects by a value of reference. Passing argument to the method means, that methodObject holds a copy of reference to finalObject (but not the copy of an object). Now, body of the method does not change or reassign finalObject object - it simply reassign the methodObject by changing a value of the reference passed to method.

Please, see also this answer - I suppose it will make it much clearer.

Community
  • 1
  • 1
Artur Malinowski
  • 1,124
  • 10
  • 30
0

you have to understand the concept of local variables here.

see picure below

enter image description here

all local variables(primitive or object) will reside in its method stack.

if its an object then only reference stays in stack and object resides in heap.

and each local variables gets place in the method stack.

so here 2 local variables gets place in stack one for each method.

and one among them is final and the other is not.

so you can reassign second one but not first one.

here in your case,

 final Object object = null

is stored in the myMethod Stack. and its declared as final here,

so within this method you cant re assign it.

however inside

private void _myMethod(Object object){
    object = new Object(); //Error here
}

Object object declaration makes the same variables to get space inside _myMethod stack place.

so here its not final hence it can be reassigned.

if you make it final inside this method then second picture in my image will have final Object variable inside its method stack then it wont allow second time assignment.

Karibasappa G C
  • 2,686
  • 1
  • 18
  • 27
0

In java variables are passed by value. In case of Objects it is the copy of the reference that is passed.

private void myMethod(){
    final Object object = null;

    _myMethod(object);
}

private void _myMethod(Object newObject){
    newObject= new Object();
}

Hence newObject is a new reference if type Object that is pointing to the same Object that object was pointing to. Note that object reference is final and not the copy i.e newObject. So what you have done is second part is perfectly valid.

public class Test{  
   final Object object = null;

private void myMethod(){    
    _myMethod(object);
}

 private void _myMethod(Object newObject){
        object = new Object(); //Error here
    }
}

Above will fail as object reference is final.

Community
  • 1
  • 1
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • Above will fail as object reference is final. is a wrong statement..and Tom answer was completely wrong so i asked him to remove his answer... object = new Object(); //Error here not because its final, its because object can not be resolved to a variable..you can see my answer below why it behaves in different way for final – Karibasappa G C May 17 '14 at 07:38
  • Edited the answer. That is what I meant. Yes object being a local variable will not be accessible in other methods scope. So changed it to instance variable. But point being the same. – Aniket Thakur May 17 '14 at 12:43
  • yes thats okie..but OP asked it on local variables which is final in one method but used to reassign in other method..he didnt ask anything on instance variable...dont know why you took it to explain – Karibasappa G C May 17 '14 at 12:46
  • I already answered OPs question in 1st part. Just wanted to demonstrate final variable cannot be instantiated for second time. In this case local but is true irrespective of that. – Aniket Thakur May 17 '14 at 12:49