Can “this” ever be null in Java?

2019-01-03 10:20发布

Saw this line in a class method and my first reaction was to ridicule the developer that wrote it.. But then, I figured I should make sure I was right first.

public void dataViewActivated(DataViewEvent e) {
    if (this != null)
        // Do some work
}

Will that line ever evaluate to false?

标签: java this
10条回答
贪生不怕死
2楼-- · 2019-01-03 10:30

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.

查看更多
别忘想泡老子
3楼-- · 2019-01-03 10:33

In static class methods, this isn't defined since this is associated with instances and not classes. I believe it would give a compiler error to attempt to use this keyword in static context.

查看更多
狗以群分
4楼-- · 2019-01-03 10:36

It's not enough that the language enforces it. The VM needs to enforce it. Unless the VM enforces it you could write a compiler that does not enforce the null check prior to calling the method written in Java. The opcodes for a instance method invocation include loading the this ref on to the stack see: http://java.sun.com/docs/books/jvms/second_edition/html/Compiling.doc.html#14787. Substituting this for a null ref would indeed result in the test being false

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-03 10:42

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;
查看更多
时光不老,我们不散
6楼-- · 2019-01-03 10:43

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 :

查看更多
Deceive 欺骗
7楼-- · 2019-01-03 10:44

When you invoke a method on null reference, the NullPointerException will be thrown from Java VM. This is by specification so if your Java VM strictly complies to the specification, this would never be null.

查看更多
登录 后发表回答