• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using THIS

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So would this be correct?

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, that would be a much more valuable lesson indeed.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic