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 trial

Java

Keyword "this" in Java

I have seen sometimes when referring to a method of a class within the class itself programmers use the keyword "this". Here in the Treehouse Java course Craig doesn't seem to use this keyword. Is there a specific reason for this? Or is it something that work both ways?

2 Answers

Michael Hess
Michael Hess
24,512 Points

It's not always just a naming convention -- that was not the best way to explain it.

The this keyword is used to overcome shadowing. The this keyword is used to refer to the field name - if the field name and parameter name are the same.

Some programmers don't like to think of a different name. So they will use name = name , but this will result in shadowing -- since both have the same name. This can be overcome by using mName = name OR this.name = name;

In Android they use mName = name; whereas a Java Developer will most likely use this.name = name;

The this keyword is also used when chaining constructors (aka explicit constructor invocation) -- that's when a constructor is called from another constructor.

This is a keyword that an object can use to refer to itself -- that's how it is used in Java.

If you would like me to explain anything else I will be happy to do so.

I get it now. So in Java the "this" keyword can be used in cases where we have chaining constructors (I haven't used these or haven't seen them so thanks for mentioning about them) or when an object is needs to refer to itself. But in the videos I see that even if we don't use "this" in the latter case (object referring to itself) the code works. Isn't that correct?

Michael Hess
Michael Hess
24,512 Points

Yes, that is correct. In the videos they are using "m" instead of "this" to ensure that the field name does not shadow the parameter name.

It sounds weird at first -- but it's really that simple!

Michael Hess
Michael Hess
24,512 Points

In Android, m stands for member -- so it's really more of a naming convention thing in Android. It also goes along with the Android track on treehouse. The this keyword is used to overcome shadowing. The this keyword is used to refer to the field name - if the field name and parameter name are the same.

For example:

public Customer(String title, String firstName, String surname,) {

        this.title = title;
        this.firstName = firstName;
        this.surname = surname;

    }

is equivalent to:

public Customer(String title, String firstName, String surname,) {

        mTitle = title;
        mFirstName = firstName;
        mSurname = surname;

    }

If you have any other questions ask away!

Thanks for answering. So the thing is if this is merely a naming convention, why do Java defines this keyword anyway. For example in the Java documentation there's a separate page for this;

https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

Is there anything that we specifically need the "this" keyword?