Results 1 to 4 of 4
  1. #1
    asherwolf is offline Member
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Default Polygons... returning and passing objects?

    Let's say I have a Polygon class and it has an ArrayList of Point2D.Double objects acting as its vertices (this.vertices).

    When I instantiate it, and then do something like this:

    Point2D.Double point = myPolygon.getVertex(2);

    I am able to change the vertex INSIDE the instantiation by doing this:

    point.setLocation(5, 5);

    Is this something to worry about? I think that I want it so that I have an ArrayList of values that can be changed inside an object, where what goes into the array from outside isn't 'linked' to anything else in this way, and what is read outside from the array isn't 'linked' to anything else in this way.

    Do I just put .clones() everywhere a value goes in and out? That seems dense and redundant to me.

    Any advice? I can post the whole Polygon class here if anyone wants to see it (or if that's possible).

    Thanks!
    Asher

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    I am able to change the vertex INSIDE the instantiation
    What are your program's requirements? Should it be able to change the contents of the object or should the contents be immutable?

    isn't 'linked'
    Can you explained what "linked" means? Java uses reference variables to point to objects.
    Many variables can point to the same object.

  3. #3
    asherwolf is offline Member
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Default

    Well, no, when you get a vertex it shouldn't be able to change the object it got the vertex from. However, I'm afraid that creating a new object every time a vertex is retrieved will be costly memory wise, maybe not. When I said linked, I meant that more than one variable can point to the same object.

    Any suggestions on how to think about this?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    it shouldn't be able to change the object it got the vertex from
    If the members are private and there are no set methods, the contents of an object should be immutable. Use get methods to read the contents of an object.
    Not sure about the "linked" problem.
    To remove a reference to an object, you can set the referring pointer to null.

Similar Threads

  1. Passing array objects
    By drymsza1234 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 02:40 PM
  2. Passing Objects
    By Java_Developer in forum New To Java
    Replies: 6
    Last Post: 10-29-2009, 01:32 PM
  3. MVC passing objects
    By simo_mon in forum New To Java
    Replies: 1
    Last Post: 07-17-2009, 06:57 AM
  4. JNI - passing and returning parameters by value
    By java_to_c in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 08:49 PM
  5. Passing objects in Java
    By jbostjr in forum Advanced Java
    Replies: 1
    Last Post: 10-30-2007, 05:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •