Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - when to use 'this' keyword [duplicate]

What is the best practise for using the this keyword in Java? For example, I have the following class:

class Foo {     Bar bar;      public Foo(Bar bar) {          this.bar = bar;     } } 

That's fine and all, but Java is clever enough to know what is happening if I change the statement in the constructor to

 bar = bar; 

So why use the this keyword? (I realise in some situations, it's totally necessary to use it, I'm just asking for situations like this). Actually, I tend to use the keyword purely for readability sake but what's the common practise? Using it all over the shop makes my code look a bit messy, for example

boolean baz; int someIndex = 5; this.baz = this.bar.getSomeNumber() == this.someBarArray[this.someIndex].getSomeNumber(); 

Obviously a poor bit of code but it illustrates my example. Is it just down to personal preference in these cases?

like image 786
jackbot Avatar asked Mar 11 '10 22:03

jackbot


People also ask

When should I use this keyword in Java?

The this keyword refers to the current object in a method or constructor. The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).

Should I always use this keyword Java?

In answer to "Are there any cases where you should always use this ?" You should use it when it is needed to avoid ambiguity, for example if there is another variable with the same name in scope.

Is it a good practice to use this keyword in Java?

The this keyword is primarily used in three situations. The first and most common is in setter methods to disambiguate variable references. The second is when there is a need to pass the current class instance as an argument to a method of another object.

What happens if I don't use this keyword in Java?

Definition and Usage If you omit the keyword in the example above, the output would be "0" instead of "5". this can also be used to: Invoke current class constructor. Invoke current class method.


2 Answers

but Java is clever enough to know what is happening if I change the statement in the constructor to

  bar = bar; 

FALSE! It compiles but it doesn't do what you think it does!

As to when to use it, a lot of it is personal preference. I like to use this in my public methods, even when it's unnecessary, because that's where the interfacing happens and it's nice to assert what's mine and what's not.

As reference, you can check the Oracle's Java Tutorials out about this.subject ;-)

http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

like image 57
polygenelubricants Avatar answered Oct 03 '22 09:10

polygenelubricants


You should use it when you have a parameter with the same name as a field otherwise you will run into issues. It will compile, but won't necessarily do what you want it to.

As for everywhere else, don't use it unless it's needed for readability's sake. If you use it everywhere, 20% of your code will consist of the word 'this'!

like image 24
Dónal Boyle Avatar answered Oct 03 '22 09:10

Dónal Boyle