• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

this-operator

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FYI ADMINS:

I was redirected here from

https://coderanch.com/forums/

a topic moved from Advanced to Beginner (but linking to the wrong one)
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic