0

I'm taking this Oracle online java class and don't understand how the this in this example refers to the

origin = new Point(0,0); 

object.

First example of the Rectangle class.

public class Rectangle {

public int width = 0;
public int height = 0;
public Point origin;

// four constructors
public Rectangle() {
origin = new Point(0, 0);
}
public Rectangle(Point p) {
origin = p;
}
public Rectangle(int w, int h) {
origin = new Point(0, 0);
width = w;
height = h;
}
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}

// a method for moving the rectangle
public void move(int x, int y) {
origin.x = x;
origin.y = y;
}

// a method for computing the area of the rectangle
    public int getArea() {
    return width * height;
    }

}

Rectangle example using the this keyword.

public class Rectangle {
private int x, y;
private int width, height;

public Rectangle() {
    this(0, 0, 1, 1);
}
public Rectangle(int width, int height) {
    this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
}
...
}

Where does the second example refer to the origin = new Point(0,0) object?

public class Point {
// TODO Auto-generated method stub
        public int x = 0;
        public int y = 0;
        // a constructor!
        public Point(int a, int b) {
        x = a;
        y = b;
}

}
jmj
  • 237,923
  • 42
  • 401
  • 438
StacyM
  • 1,036
  • 5
  • 23
  • 41
  • What is your exact question? Are you talking about `this(...)` call? – Braj Jun 25 '14 at 22:00
  • Ok I understand, couldn't they have gotten rid of the first two contructors? Also, while using "this", does this get rid of the Point object? – StacyM Jun 25 '14 at 22:02
  • @Jigar Joshi It's not duplicate. OP talking about `this` used for instance member. – Braj Jun 25 '14 at 22:02

2 Answers2

2

If you don't add this keyword then it is by default (implicitly) added for instance members of the class.

So its the same thing whether you use this keyword or not.

Please have a look at Understanding Class Members

Read another post on Using the keyword “this” in java

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Yes, I know both examples are the same. I didn't understand where there is reference to the Point object in the "this" example... – StacyM Jun 25 '14 at 22:07
  • Sorry It's not clear to me. What are you asking? Please explain a bit more. What line are you talking about in the code? – Braj Jun 25 '14 at 22:08
  • In the non-"this" example, we set our first constructor to use public class Point(). Where is this in the second example using "this"? Ugh! I don't see this.origin = new Point(0,0) anywhere in the second example. – StacyM Jun 25 '14 at 22:13
  • See biggus's comment. – StacyM Jun 25 '14 at 22:18
  • @StacyM Your question is some what confusing. You have mixed a lot of things. Next Time Use `???` in second example where you have doubt in your code to make if more clear. – Braj Jun 25 '14 at 22:20
1

Looks like the second example doesn't refer to the Point class.

For some odd reason, they apparently just changed the way that the class works, replacing the "Point" class (which is effectively just two integers anyway) with integers x and y

To re-write the second class such that it actually works the same as the first, you'd write it as follows:

public class Rectangle {
private Point origin;
private int width, height;

public Rectangle() {
    this(new Point(0,0), 1, 1);
}
public Rectangle(int width, int height) {
    this(new Point(0,0), width, height);
}

//One more constructor just for fun
public Rectangle(int x, int y, int width, int height) {
    this(new Point(x, y), width, height);
}

//Note: All of the constructors are calling this method via this(Point, int, int)
public Rectangle(Point origin, int width, int height) {
    this.origin = origin;
    this.width = width;
    this.height = height;
}
...
}
biggusjimmus
  • 2,706
  • 3
  • 26
  • 31
  • Yay! I thought that was strange. So, are you saying the second example listed by the course, would not work? – StacyM Jun 25 '14 at 22:18
  • It's not that it *wouldn't work*, it's just that it uses a different method of representing the origin. The Point class is just a class that has integer properties "x" and "y". It can be helpful from a design perspective to have classes like this, but it's not necessary. Then again, and maybe more to your point, if the second class references the property "origin," then it's going to have a compile error, since that property doesn't exist in your provided code. – biggusjimmus Jun 25 '14 at 22:20