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 trialColby Wise
3,165 PointsGreat Write Up on Comparable Interface & Sorting
http://javahungry.blogspot.com/2013/08/difference-between-comparable-and.html
Any idea how and why [this.]<args> = <args> is used in the below example code taken from the blog? It seems like it's to instantiate private variables but I'm not sure - any clarification would be helpful
public class Country implements Comparable<Country>{
int countryId;
String countryName;
public Country(int countryId, String countryName) {
super();
this.countryId = countryId;
this.countryName = countryName;
}
1 Answer
Karim Zibari
7,358 PointsSince the class members fields 'countryId' and 'countryName' have have the same names as the Constructor arguments, you can use the keyword 'this' to indicate that you are referring to the class/object member fields, and not the arguments to the constructor.