• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to use Point(Point p)

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Popeye has his spinach. I have this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic