• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use Point(Point p)  RSS feed

 
Mohammad Mah
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I was reading the oracle java tutorial under:
http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html

Here's the code for the Point class:



and in the Rectangle class you have the following constructor:



If we create a new Point object like this:



and then a new Rectangle object like this:



Will that set originOne to point to the object Point at (23, 94).

I just want to make that this is the meaning of this statement:
Point(Point p)
Constructs and initializes a point with the same location as the specified Point object.

Thanks,
Mohammad
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do realise that Point object is a classic example of poor design because it has public non‑final fields?

The last bit is called a copy constructor. It creates a new object of the same class identical to the original. So if you can writeIn the case of the Point class you can implement it like thisAs for originOne, you are getting confused. No, that does not set originOne to the other Point; originOne already points to a Point object at 23, 94. What it does is set the corresponding reference in the Rectangle object to point to that Point too.
Another bit of bad design; if you change the state of the Point object, the Rectangle object will move to follow it.
I challenge you to alter their Rectangle class to get rid of that problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!