• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this-operator

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is best-practice:

Should I write the "this"-Operator explicitly or can I delete it?

What is better?



or



I know, it makes no difference but I have a lot of this.-in my java-files and want to delete the "this".

Should I do that?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's up to you, it depends on what style you personally prefer. If you add the "this", it makes it explicitly clear that you're calling a method on the current object. Personally, I prefer to not use the "this" too much in Java.

Note: "this" is not an operator, it's simply a reference to the current object.
[ October 24, 2008: Message edited by: Jesper Young ]
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI ADMINS:

I was redirected here from

http://www.coderanch.com/forums/

a topic moved from Advanced to Beginner (but linking to the wrong one)
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider an example

if you don't use this.i here with instance variable then constructor will explicitly assign the value of local variable i to itself.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree; that is a situation where the this keyword is necessary. [pedantic mode]But it's not a local variable you are reasigning; it's the parameter being reassigned to itself.[/pedantic mode]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic