Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference of this.xxx and xxx  RSS feed

 
Michelle Joe
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class ClassABC
{
private String name;
public void TestThis()
{
name = "ABC";// Line 1
this.name = "ABC";// Line 2
}
}
I am curious to know what is the difference of Line 1 and Line 2.
Or this is just another good practice to use Line 2 instead of Line 1?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no difference, and I wouldn't consider it a good practice, either. The "this.xxx" form is useful if just xxx would be ambiguous or wrong -- i.e.,

In other situations, using "this." just adds clutter and I wouldn't recommend it.
[ January 19, 2004: Message edited by: Ernest Friedman-Hill ]
 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Try this one.
this.name refers to a field's value in the object, name refers to your local field in the method.
Try to keep the names of methods and fields always in lowercase, uppercase
names always refer to classes.
Bye,
J.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other situations, using "this." just adds clutter and I wouldn't recommend it.
I realize that I am in the minority here (at least, I think that I am), but for a variety of reasons, I insist upon always using "this"
  • It makes every statement read like a sentence: <Subject>.<verb> This construct furthers the concept of self-documenting code.
  • Especially if you work in C++ as well as Java, this provides consistency across languages. While it is true that in C++ "this" is optional, there can be greater confusion if you do not use it (the called method could be a non-class function...)
  • Other Object-Oriented languages (such as Objective-C) require that every statement have a subject. In Objective-C, the reference is self, but it is required. Using this in Java provides uniformity of style across languages (if you do that sort of thing...)


  • Of course, I'm also the one who is a proponent of always enclosing single-statement "if" clauses with curly braces

    and I'm a proponent of always ending a method with a return, even if the method returns void, so take anything I say with at least three grains of salt.
     
    Marilyn de Queiroz
    Sheriff
    Posts: 9080
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm with Ernest. If you're passing in a parameter in your constructor and you want to assign that value to the member variable, it's a good time to use "this".
    Rather than:
     
    Michelle Joe
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the great response. I appreciate it very much. Thanks again.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!