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 Data Structures - Retired Getting There Class Review

What is the diffrent these two constructor..

What is the diffrent between these:

public class Treet{
    private String mAuthor; //ofröändring som inte går att ändras!
    private String mDescription;
    private Date mCreationDate;

    public Treet(String author, String description, Date CreationDate){

       mAuthor = author;
       mDescription = description;
       mCreationDate = Creation;
    }
}

to this

public class Treet{
    private String mAuthor; //ofröändring som inte går att ändras!
    private String mDescription;
    private Date mCreationDate;

    public Treet(String author, String description, Date CreationDate){

      this.mAuthor = author;
      this.mDescription = description;
      this.mCreationDate = Creation;
    }
}

is it the same or?

3 Answers

Yes, I think you're right. The compiler can't tell which x is which; that's what the this keyword does; it defines that this.x is the member variable and assumes that the lone x is the parameter.

The example is poor code - deliberately creating confusing scenarios helps no one. If the compiler doesn't know what you mean, what chance does the next programmer to read your code stand!! :smile:

However, your example demonstrates very well what this is used for - as in my first response, it distinguishes between the member variable inside the class and the parameter passed in or a local variable.

The reality is that adopting a clear naming convention for variables avoids this confusion completely, negating the need to use the this keyword, except where it adds further clarity. Avoiding having single letter variables (except in for loops where that is convention) and using descriptive names helps everyone understand what the code is trying to achieve.

Make sense?

Steve.

Hi there,

Yes, I think they would produce the same result. Adding the this keyword ( other languages use self in a similar way) is often done to distinguish between parameter names and member variable names as they are often similar.

However, in both your constructors, you are passing in CreationDate as parameter but then only assigning Creation to mCreationDate. That won't work as Creation is undefined.

I hope that helps, else just shout back with your questions and I'll see if I can answer them!

Steve.

Hi!

Thanks! Ok but .. what about this one

public class Tutorial{
int x y;

public Tutorial(int x, int y){
x = x;
this.y = y;
}

java says that x is zero.. this is really confusing "this"

but is because IF I have same name in constructor parameter like instance variable then java will be confused am I right?