There's a perfectly reasonable implementation for "this = null" -- i.e., perfectly valid bytecode that could be emitted, if it weren't illegal, because "this" is just local variable #0 -- so that certainly seems possible. Yes, it was a bug in the Eclipse compiler.
tl;dr, "this" can only be called from a non-static method and we all know that a non-static method is called from some sort of object which cannot be null.
The value 0 (all bits at zero) is a typical value used in memory to denote null . It means that there is no value associated with name . You can also think of it as the absence of data or simply no data. The Java Virtual Machine specification does not mandate a concrete value encoding null .
== and !=The comparison and not equal to operators are allowed with null in Java. This can made useful in checking of null with objects in java.
No it can't. If you're using this
, then you're in the instance so this
isn't null.
The JLS says :
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.
If you invoked a method from an object, then the object exists or you would have a NullPointerException
before (or it's a static method but then, you can't use this
in it).
Resources :
this
keywordIt's like asking yourself "Am I alive?" this
can never be null
No never, the keyword 'this' itself represents the current alive instance (object) of that class within the scope of that class, with which you can access all its fields and members (including constructors) and the visible ones of its parent class.
And, more interestingly, try setting it:
this = null;
Think about it? How can it be possible, won't it be like cutting the branch you are sitting on. Since keyword 'this' is available within the scope of the class thus as soon as you say this = null; anywhere within the class then you are basically asking JVM to free the memory assigned to that object in the middle of some operation which JVM just can't allow to happen as it needs to return back safely after finishing that operation.
Moreover, attempting this = null;
will result in compiler error. Reason is pretty simple, a keyword in Java (or any language) can never be assigned a value i.e. a keyword can never be the left value of a assignment operation.
Other examples, you can't say:
true = new Boolean(true);
true = false;
If you compile with -target 1.3
or earlier, then an outer this
may be null
. Or at least it used to...
No. To call a method of an instance of a class, the instance has to exist. The instance is implicitly passed as a parameter to the method, referenced by this
. If this
was null
then there'd have been no instance to call a method of.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With