Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of Java this keyword

Tags:

java

this

In a class constructor, I am trying to use:

if(theObject != null)
    this = theObject; 

I search the database and if the record exists, I use theObject generated by Hibernate Query.

Why can't I use this?

like image 236
Dustin Sun Avatar asked Nov 20 '10 14:11

Dustin Sun


2 Answers

It's because 'this' is not a variable. It refers to the current reference. If you were allowed to reassign 'this', it would no longer remain 'this', it would become 'that'. You cannot do this.

like image 89
Sid Avatar answered Oct 23 '22 07:10

Sid


this is not a variable, but a value. You cannot have this as the lvalue in an expression.

like image 20
AbdullahC Avatar answered Oct 23 '22 05:10

AbdullahC