7

What is the value of this? somewhere I read that in C# (this==null) is possible. But what about in Java? That, will the following fragment ever return true?

 if(this!=null)
 {
     return false;
 }
 else
 {
     return true;
 }
Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • For those who (like me) can' grok the idea that `this` *could be* `null`, read this Q&A - http://stackoverflow.com/questions/3143498/why-check-this-null – Stephen C Jun 29 '13 at 09:58

7 Answers7

7
if(this!=null)

The above always evaluates to true, meaning that the first branch of your if will always get executed, and the function always returns false.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • @StinePike Certainly. Did someone suggest otherwise? In this answer? Your point? – user207421 Jun 29 '13 at 10:26
  • @EJP .. oh at first npe wrote that it is not valid.. I didn't see that he changed the text.. So as he modified, I have deleted that comment :) – stinepike Jun 29 '13 at 10:28
  • @EJP: At first the question asked about `this=!null`, to which I said the syntax was invalid. Then the `=!` got changed to `!=`, so I edited the answer to match the question. Hence the confusion. – NPE Jun 29 '13 at 11:41
1

this can never be null. Because this refers to the self instance of the object. And it is only accessed when a object is already created.

so the else block is unreachable.

keyser
  • 18,829
  • 16
  • 59
  • 101
stinepike
  • 54,068
  • 14
  • 92
  • 112
1

"this" can never be null in Java

.....?

if(this!=null)
{
 return false;
}
Ganesh Rengarajan
  • 2,006
  • 13
  • 26
1

this means current object which can never be null.

M Sach
  • 33,416
  • 76
  • 221
  • 314
0

Within an instance method or a constructor, this is a reference to the current object .So it never be null.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Never false, you mean. The test is != null. – user207421 Jun 29 '13 at 09:57
  • No,@EJP the instance.I mentioned this also now.It retruns always false. – Suresh Atta Jun 29 '13 at 09:59
  • What 'instance'? The test in the OP's code is (this!= null). This test can never be false. 'Since it is a valid syntax' has nothing to do with it either way. – user207421 Jun 29 '13 at 10:01
  • @EJP - false is true for small enough values of true :-) – Stephen C Jun 29 '13 at 10:02
  • It is now neither right nor wrong, just meaningless. You have failed to express yourself clearly. @StephenC *de minimis curat non lex.*. :-| – user207421 Jun 29 '13 at 10:07
  • True EJP..Let me know,if you found any mistake. – Suresh Atta Jun 29 '13 at 10:13
  • Not much of an improvement. It isn't English, and it doesn't really explain anything. 'this' refers to the current object, which *is* the current context, to the extent that 'current context' means anything. – user207421 Jun 29 '13 at 10:15
  • Normally I would upvote a corrected answer that I had downvoted, but it shouldn't take four edits, several of which made an already meaningless answer worse, to get to a correct answer, so I am making an exception. If you don't know the answer, or can't express it clearly, leave it to others. – user207421 Jun 29 '13 at 10:32
  • sure EJP,thanks for the suggestion. English in not my native language.I tried to give an answer. – Suresh Atta Jun 29 '13 at 10:33
0

"this" keyword refers to the "that" object, which you are referring to..

class Sample
{
  int age; 
  Sample(int age)
  {
    this.age = age;   // this.age -> the variable a in the that current instance
  }
  public void display()
  {
     System.out.println(age);  //age here is actually this.age
  }
}
public class XYZ
{
 public static void main(String[] args)
 {
   Sample a,b;
   a.display();
   b.display();
 }
}
Ravitheja
  • 142
  • 1
  • 8
0

Just think logically - that's like saying If I don't exist....

The thing that currently has control of the code has to exist, otherwise the code wouldn't be running in the first place.

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • Don't make generalizations like that. In some languages, `this` may in some circumstances be null. – newacct Jun 29 '13 at 20:04