Click here to Skip to main content
15,912,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am confused about why do we use this keyword when we can do same by directly referring using the attribute name. Below is example where I used name and this.name. What is the point in using this keyword if same can be achieved other way.


Java
class Myclass
{
    String name;
    public Myclass(String n1)// n1 represents name
    {
        name=n1;
    }

    public Myclass(String firstname,String lastname)
    {
        this.name = firstname + " " + lastname;
    }
}
Posted

Its not a good example of the use of the "this" keyword. Typically you wouldn't do what you wrote, at least not in clean code.

"this" is used to pass the instance of the class to other functions. You can use the "this" keyword to reference the current instance of the class or pass to other subroutines, but you typically wouldn't use the "this" keyword to access data in the class you are working in. Its not a violation of the keyword, but just doesn't add any value.

[Edit]

I'm not sure if this is valid in Java, but in c# we can also use it in the following way:

Java
class Myclass
{
    String firstname;
    public Myclass(String n1)// n1 represents name
    {
        firstname=n1;
    }
 
    public Myclass(String firstname,String lastname)
    {
        this.firstname = firstname + " " + lastname;
    }
}


To differentiate between the parameter and the class member.

[/Edit]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 25-Jul-13 21:26pm    
That's exactly the case where you resolve the name with "this", 5ed.
—SA
Ron Beyer 25-Jul-13 21:38pm    
Thanks Sergey
Sergey Alexandrovich Kryukov 25-Jul-13 21:33pm    
I also added my own answer with 3 referenced to my past answers, and I credited this answer and advised OP to accept it, not Solution 2.
—SA
First of all, so far, the only correct solution is the Solution 1; you should accept Solution 1 formally, and probably should reject Solution 2: readability is important, but resolution of the names is critically important and the case where the lack of "this" would case compilation error is overlooked in Solution 2.

Now, I think you also should understand what "this" actually is. It's is not so trivial as it may seem. Please see my past answer:
C# windows base this key word related and its uses in the application[^].

This is related to instance methods, so please also see:
What makes static methods accessible?[^],
Catch 22 - Pointers to interface objects die when function using them is made static.[^].

(Please don't get confused with some C# and C++ code samples. They explain some first principles which are equally applied to Java and other OPP languages.)

Good luck,
—SA
 
Share this answer
 
v3
Comments
Ron Beyer 25-Jul-13 21:38pm    
+5'd, although I think you transposed the "accept solution 2 and reject solution 1" :)
Sergey Alexandrovich Kryukov 25-Jul-13 21:39pm    
Thank you, Ron.
Solution 1.. 2...
OOPS! Thank you for the note, I'll fix it. :-)
—SA
As you stated, this is not needed and using the member name without this give the same result. So from the technical standpoint it is not necessary to use this.

However it can be used to make it clear in the code that you are referring to a member variable of an instance of the class and not a (static) class variable. This improves the readability of your code if you follow this convention across your project.
 
Share this answer
 
I don't know why anyone else would, but I do because I like to see which items are instance items and which aren't (static, local, etc.) -- but as presented provides little actual benefit.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900