Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #26227

Re: Object's properties changed

Date 2011-02-12 15:01 +0800
From Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com>
Newsgroups comp.lang.java.programmer
Subject Re: Object's properties changed
References <0c26a62d-08b7-4149-a36b-ec3d9e6e8804@o14g2000prb.googlegroups.com>
Message-ID <d62dnUWFX6XMrcvQnZ2dnUVZ_tidnZ2d@posted.palinacquisition> (permalink)

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web