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 Java Objects Meet Objects Constructors

Clause this. in the return statement

If we are using this to refer to the private statement, don't we have to use it in this block of code?:

public String getCharacterName() { return characterName; }

public String getCharacterName() { return this.characterName; }

1 Answer

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Great question Fernando Secchi !

this is always assumed and only required to be more specific. For instance:

public class Example {
   private String item;

   public String getItem() {
       // Not needed here because it is the only item in scope
       return item;
   }

   public void setItem(String item) {
         // Needed here to show that difference between the item passed in through the argument
        this.item = item
    }
}

Make sense?

Amazing

Had this exact thought