Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #26124 > unrolled thread
| Started by | Vasu <vasudevmukherjee@yahoo.co.uk> |
|---|---|
| First post | 2011-02-11 21:55 -0800 |
| Last post | 2011-02-12 15:01 +0800 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.java.programmer
Object's properties changed Vasu <vasudevmukherjee@yahoo.co.uk> - 2011-02-11 21:55 -0800
Re: Object's properties changed Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-02-12 15:01 +0800
| From | Vasu <vasudevmukherjee@yahoo.co.uk> |
|---|---|
| Date | 2011-02-11 21:55 -0800 |
| Subject | Object's properties changed |
| Message-ID | <0c26a62d-08b7-4149-a36b-ec3d9e6e8804@o14g2000prb.googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> |
|---|---|
| Date | 2011-02-12 15:01 +0800 |
| Message-ID | <d62dnUWFX6XMrcvQnZ2dnUVZ_tidnZ2d@posted.palinacquisition> |
| In reply to | #26124 |
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 already in the JDK? 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web