Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMichael Hughes
Courses Plus Student 1,847 PointsUsing the Java keyword 'This'
When creating the public constructor for the Treet object, why don't I used the keyword 'this' in order for the values entered in the constructor to point to referencing the object's respective member variables? In other words, why wouldn't I use:
this.mAuthor = author;
in the body of the constructor? Is it redundant?
4 Answers
Ken Alger
Treehouse TeacherMichael;
This is a great question and it kind of boils down to somewhat of a style preference if you are referring to variables with the same name. That's not your exact question, but here's an example of the difference:
class SomeClass {
private final String name;
public SomeClass(final String name) {
this.name = name;
}
}
In this example the variable named name
is a member variable in the class SomeClass
and in the function (Constructor) it is assigned to the argument passed in. You need to utilize the keyword this
so that the member variable name
is assigned to the argument variable name
that was passed into the function.
From a functional standpoint you could also do:
class SomeClass {
private final String mName;
public SomeClass(final String name) {
mName = name;
}
}
This will produce the same results. Can you see how, in this example, mName
and this.name
are referencing the same memory slot in the computer?
Some people don't like a bunch of prefixes and they prefer to go with this.name
, others find mName
easier to read. It really kind of depends on the developer and the company for whom they are working. You tend to find the m
prefix used a lot in Android development.
I hope that offers some insight in addition to the answers Chris Casey and Chris Howell provided.
Happy coding,
Ken
Chris Casey
11,656 PointsYou would use "this" if the variable names were the same. "this.author = author;" for example. Since the member variable is "mAuthor" the "this" keyword is not needed.
Michael Hughes
Courses Plus Student 1,847 PointsAwesome, thanks for the quick response!
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsIt was a quick response indeed. He beat me to it! :)
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsIt has been awhile since I have worked with Java, I have been on a Python tangent lately. But I too remember using the keyword 'this'. But I think the only time you really would need to use it is when you are using a variable with the same exact name in the class (at the top) then again inside of a method. So if you were using 'author' instead of 'mAuthor', then using 'author' again inside the body of the constructor. Then you would want to refer to the variable at the top as 'this.author'.
Michael Hughes
Courses Plus Student 1,847 PointsThanks for the response Chris! I appreciate it!
Cristiano Monteiro
5,942 Pointsha just asked myself the same question. Great answers Thanks!
Michael Hughes
Courses Plus Student 1,847 PointsMichael Hughes
Courses Plus Student 1,847 PointsThis is a super thorough and excellent answer, thanks for that and including your code Ken!