Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is assignment to 'this' not allowed in java?

Tags:

The error I get from the compiler is "The left hand side of an assignment must be a variable". My use case is deep copying, but is not really relevant.

In C++, one can assign to *this.

The question is not how to circumvent assignment to this. It's very simple, but rather what rationale is there behind the decision not to make this a variable.

Are the reasons technical or conceptual?

My guess so far - the possibility of rebuilding an Object in a random method is error-prone (conceptual), but technically possible.

EDIT Please restrain from variations of "because java specs say so". I would like to know the reason for the decision

like image 528
kostja Avatar asked Nov 02 '11 11:11

kostja


People also ask

Can you assign this in Java?

According to the definition "this" is a keyword which acts as a reference to the current object (the object from whose constructor/method you are using it), its value id is fixed. Therefore, you cannot assign a new reference value to it. Moreover, it is just a keyword, not a variable.

What happens if you dont use this in Java?

Definition and Usage The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter). If you omit the keyword in the example above, the output would be "0" instead of "5".

Is it mandatory to use this in Java?

Its compulsory to use this if you have class variable and function parameter of same type.

Can you do multiple assignment in Java?

Java permits the use of multiple assignments in one statement. In such statements, the assignment operators are applied from right to left, rather than from left to right.


2 Answers

In C++, one can assign to *this

Yes, but you can't do this = something in C++, which I actually believe is a closer match for what you're asking about on the Java side here.

[...] what rationale is there behind the decision not to make this a variable.

I would say clarity / readability.

this was chosen to be a reserved word, probably since it's not passed as an explicit argument to a method. Using it as an ordinary parameter and being able to reassign a new value to it, would mess up readability severely.

In fact, many people argue that you shouldn't change argument-variables at all, for this very reason.

Are the reasons technical or conceptual?

Mostly conceptual I would presume. A few technical quirks would arise though. If you could reassign a value to this, you could completely hide instance variables behind local variables for example.

My guess so far - the possibility of rebuilding an Object in a random method is error-prone (conceptual), but technically possible.

I'm not sure I understand this statement fully, but yes, error prone is probably the primary reason behind the decision to make it a keyword and not a variable.

like image 99
aioobe Avatar answered Oct 21 '22 08:10

aioobe


because this is final,

this is keyword, not a variable. and you can't assign something to keyword. now for a min consider if it were a reference variable in design spec..and see the example below

and it holds implicit reference to the object calling method. and it is used for reference purpose only, now consider you assign something to this so won't it break everything ?

Example

consider the following code from String class (Note: below code contains compilation error it is just to demonstrate OP the situation)

   public CharSequence subSequence(int beginIndex, int endIndex) {       //if you assign something here        this = "XYZ"  ;        // you can imagine the zoombie situation here       return this.substring(beginIndex, endIndex);    } 
like image 33
jmj Avatar answered Oct 21 '22 10:10

jmj