Why is assignment to 'this' not allowed in

2019-01-11 13:35发布

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

7条回答
疯言疯语
2楼-- · 2019-01-11 14:16

this isn't even a variable. It's a keyword, as defined in the Java Language Specification:

When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked (§15.12), or to the object being constructed

So, it's not possible as it's not possible to assign a value to while.

查看更多
登录 后发表回答