Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using THIS

 
Rich Nelson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have a homework assignment which prompts me to use the following code...



...and re-write these constructors using the "this" reference. I can't seem to grasp the concept of using "this", however. Could someone please explain what needs to be done here, and give me an example of how I could solve at least one of these methods in this way?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes I like to think of object instances as people. The code in the class is a set of instructions the person must follow. Imagine I round up a person to act as an object and I hand him a hat and say "put on this hat". He's probably going to assume I mean put it on his own head. But maybe he's sitting next to somebody who really needs a hat, and he puts it on their head. Then I'd clarify the instructions "put this hat on your own head" Now he can't make that mistake.

When the code is running, "this" is a lot like "your own". In your class, you're assigning a value to "a". The object looks around and the only "a" it can see is its own variable, so there's no question what you mean. You can still write the code to say "your own a" even if it's not really necessary.

So when is it necessary? When there might be some confusion about which "a" we mean. This is common in constructors:

Read up on visibility and scoping to see what the compiler would think if you left "this" out of that constructor. Find out if "this" is necessary or just a favor for future readers?
 
Rich Nelson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a great way of explaining it, thanks alot.
 
Rich Nelson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So would this be correct?

 
Henry Wong
author
Marshal
Pie
Posts: 21005
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend that you ask your instructor to clarify the assignment a bit more. Based on the number of constructors, and how many are just variations of others... you instructor could also be talking about using the this() syntax to have your constructors call each other.

Henry
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, that would be a much more valuable lesson indeed.
 
Martha Cofran
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not do both to make sure you've met your instructors expectations?

public class X {

private int a, b, c;

public X() {
this(0);
}

public X(int n) {
this(n, n);
}

public X(int n, int m){
this(n, m, 1);
}

public X(int n, int m, int p){
this.a = n;
this.b = m;
this.c = p;
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic