0

I've learned that inside a non-static block, this represents the current reference on which that block is invoked. My doubt is is there any need of checking this is ever null or not, I mean if it is null in the first place the block(method, constructor, etc) won't be executed (NPE). I came across this code where it checks this != null. It don't know why it is used, is there actually a need?

public int compareTo(StudentMarksTO arg0) {
    if(arg0.isIsfalsegenerated()==true) {
        if(arg0!=null && this!=null && 
           arg0.getFalseNo()!=null && this.getFalseNo()!=null &&
           !arg0.getFalseNo().toString().equals("") && 
           !this.getFalseNo().toString().equals("") ) {
        return this.getFalseNo().compareTo(arg0.getFalseNo());
    }
    else
        return 0;             
    }
}
Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52

1 Answers1

1

If you're using this, then you're in the instance so this isn't null.

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.

for more info check this out Can "this" ever be null in Java?

Solomon
  • 626
  • 1
  • 5
  • 16