Object's properties changed

Discussion in 'Java' started by Vasu, Feb 12, 2011.

1. VasuGuest

Hi There! If somebody can help me solving following problem:

We have 3 objects (rectOne, rectTwo and rect3) of rectangle class
which also refers to point class for starting position of rectangle.
Now, we have defined starting position of rectangles as 23, 94, now we
change the starting position for rectTwo rectangle to 40 and 72, but
we find that starting position of rectOne and rect3 also change
whereas THAT WAS NOT INTENDED, so how do I achieve it ? Here is the
code and output :
class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int a, int b) {
x = a;
y = b;
}
}

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;
}
}

public class CreateObjectDemo {
public static void main(String[] args) {

//Declare and create a point object
//and 3 rectangle objects.
Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 20);
Rectangle rectTwo = new Rectangle(50, 100);
Rectangle rect3 = new Rectangle(45, 45);

//display rectOne's y point
System.out.println("Original y point of rectOne: " +
rectOne.origin.y);

//set rectTwo's position
rectTwo.origin = originOne;

//display rectTwo's position
System.out.println("Original X Position of rectTwo: " +
rectTwo.origin.x);
System.out.println("Original Y Position of rectTwo: " +
rectTwo.origin.y);

//display rect3's width, height, and area
rect3.origin = originOne;
System.out.println("Original y point of rect3: " +
rect3.origin.y);

//move rectTwo and display its new position
System.out.println("Now we are moving rectTwo to a different place");
rectTwo.move(40, 72);
System.out.println("New X Position of rectTwo: " +
rectTwo.origin.x);
System.out.println("New Y Position of rectTwo: " +
rectTwo.origin.y);

//display rect3's width, height, and area
System.out.println("New y point of rect3: " + rect3.origin.y);

//display rectOne's y point
System.out.println("New y point of rectOne: " +
rectOne.origin.y);

}
}

Original y point of rectOne: 94
Original X Position of rectTwo: 23
Original Y Position of rectTwo: 94
Original y point of rect3: 94
Now we are moving rectTwo to a different place
New X Position of rectTwo: 40
New Y Position of rectTwo: 72
New y point of rect3: 72
New y point of rectOne: 72

Vasu, Feb 12, 2011

2. VasuGuest

On Feb 12, 12:01 pm, Peter Duniho <>
wrote:
> On 2/12/11 1:55 PM, Vasu wrote:
>
> > Hi There! If somebody can help me solving following problem:

>
> > We have 3 objects (rectOne, rectTwo and rect3) of rectangle class
> > which also refers to point class for starting position of rectangle.
> > Now, we have defined starting position of rectangles as 23, 94, now we
> > change the starting position for rectTwo rectangle to 40 and 72, but
> > we find that starting position of rectOne and rect3 also change
> > whereas THAT WAS NOT INTENDED, so how do I achieve it ?  [...]

>
> Among other things, stop using the same Point object to describe the
> origin for all three of your Rectangle instances.
>
> Frankly, I can't imagine what would motivate someone to define their own
> custom Point and Rectangle structures anyway. What's wrong with the ones
>
> But if you're going to define your own, and you're going to use public
> instance fields (YUCK!) that reference mutable objects, then you need to
> make sure the code you write that uses your data structures does so in
> the right way, by either not sharing the same mutable object among more
> than one Rectangle, or by not mutating a mutable object that is shared.
>
> I don't like the public fields in the JDK types, but at least they are
> always value types, and thus not sharable with other instances of the
> types.  Your problem would never have even come up if you'd just stuck
> with the JDK types.
>
> Pete

Hi Peter, thanks for your prompt reply, actually I'm newbie in Java
programming field and just learning and I just tried the example @
Oracle / java programming basics. Thanks anyway once again and I will
find how to achieve the objectives. But if you could suggest the code
to be used here, will be highly appreciable.

Vasu, Feb 12, 2011

3. LewGuest

Peter Duniho wrote:
>> Among other things, stop using the same Point object to describe the
>> origin for all three of your Rectangle instances.
>> ...

Vasu wrote:
> Hi Peter, thanks for your prompt reply, actually I'm newbie in Java
> programming field and just learning and I just tried the example @
> Oracle / java programming basics. Thanks anyway once again and I will
> find how to achieve the objectives. But if you could suggest the code
> to be used here, will be highly appreciable.

You point all three rectangles to the SAME origin. Change the origin, all
three move. He told you not to use the same 'Point' to describe all three

--
Lew
Ceci n'est pas une fenÃªtre.
..___________.
|###] | [###|
|##/ | *\##|
|#/ * | \#|
|#----|----#|
|| | * ||
|o * | o|
|_____|_____|
|===========|

Lew, Feb 12, 2011
4. Guest

In article <>,
Peter Duniho <> wrote:
> On 2/12/11 1:55 PM, Vasu wrote:
> > Hi There! If somebody can help me solving following problem:
> >
> > We have 3 objects (rectOne, rectTwo and rect3) of rectangle class
> > which also refers to point class for starting position of rectangle.
> > Now, we have defined starting position of rectangles as 23, 94, now we
> > change the starting position for rectTwo rectangle to 40 and 72, but
> > we find that starting position of rectOne and rect3 also change
> > whereas THAT WAS NOT INTENDED, so how do I achieve it ? [...]

>
> Among other things, stop using the same Point object to describe the
> origin for all three of your Rectangle instances.
>
> Frankly, I can't imagine what would motivate someone to define their own
> custom Point and Rectangle structures anyway.

As a way to learn about writing classes?

Based on later posts in this thread, that might not really apply in
this situation, since the example the OP is working from assumes use
of the library classes.

Still, as someone who teaches programming classes I think there can be
value for beginners in reinventing the wheel. <shrug>

(I agree with you otherwise. "Just sayin'" about potential pedagogical
value, maybe.)

> What's wrong with the ones
>
> But if you're going to define your own, and you're going to use public
> instance fields (YUCK!) that reference mutable objects, then you need to
> make sure the code you write that uses your data structures does so in
> the right way, by either not sharing the same mutable object among more
> than one Rectangle, or by not mutating a mutable object that is shared.
>
> I don't like the public fields in the JDK types, but at least they are
> always value types, and thus not sharable with other instances of the
> types. Your problem would never have even come up if you'd just stuck
> with the JDK types.
>
> Pete

--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.

, Feb 12, 2011