Jump to content
  • Advertisement
Sign in to follow this  
hisDudeness

object references

This topic is 4545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing in Java and want to make sure I understand how object references work. Observe: class Widget { /* blah */ } Widget a = new Widget(); a.xyz = 15; a.hasSpinach = true; Widget b = a; Now if I change the properties of Widget 'b': b.xyz = 40; Does that mean I also changed the value of 'xyz' in Widget 'a'??

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
No. Objects are only by reference when used as a function argument.

Widget b = a; just creates a copy of a.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
No. Objects are only by reference when used as a function argument.

Widget b = a; just creates a copy of a.


not in java. it will behave as hisDudeness describes

Share this post


Link to post
Share on other sites
I'll second rip-off's response, just to reinforce that his is the right response. The ONLY time an expression like "b = a" leads to a copy of a is if variables a and b are of a primitive type, which in Java are int, float, double, and boolean. The only way to copy an object is to use the clone method or to write your own method to create a copy of it.

Share this post


Link to post
Share on other sites
Quote:
Original post by hisDudeness
I am writing in Java and want to make sure I understand how object references work.

Observe:

class Widget { /* blah */ }

Widget a = new Widget();
a.xyz = 15;
a.hasSpinach = true;

Widget b = a;

Now if I change the properties of Widget 'b':

b.xyz = 40;

Does that mean I also changed the value of 'xyz' in Widget 'a'??


Yes.

Share this post


Link to post
Share on other sites
Quote:
Original post by hisDudeness
Widget a = new Widget();
a.xyz = 15;
Widget b = a;
b.xyz = 40;

Does that mean I also changed the value of 'xyz' in Widget 'a'??

Yes, because both a and b do not contain Widget-Objects but merely references to Widget-Objects.

Share this post


Link to post
Share on other sites
If you wanted to create a copy of a and store it in b you should implement an override of Object.clone - this method is inherited by all classes (since in Java every class is a superclass of java.lang.Object):

class Widget {
private int member_;

public Widget clone() {
Widget newWidget = new Widget( this.member_ );
return newWidget;
}

public Widget(int val) {
this.member_ = val; }

// ...etc..
}

Now you can use this function to "copy-construct" a new instance of Widget:

Widget a = new Widget(42); // creates a new widget
Widget b = a.clone(); // creates a new widget with same state as a
b.setMember(1337); // sets b's member (which was 42) to 1337.
// a's member is unchanged, because b != a.

Yay :D


EDIT: Yeah, I'm an idiot. Object.clone should have a return type of Object, not Widget -

public Object clone() { .. }

Sorry 'bout that :/

[Edited by - Mushu on January 10, 2006 6:23:49 PM]

Share this post


Link to post
Share on other sites
Okay so I'm looking to take Mushu's advice on cloning because I want to simplfy create exact copies of my objects, instead of creating two object references that manage the same object.

Mushu, using the source code you provided I got a compilation error:


class Widget
{
int member;

Widget(int val)
{
member = val;
}

Widget clone()
{
Widget w = new Widget(member);
return w;
}
}



javac is complaining that I cannot override Object's clone() method because access to it is protected. Now the only way my source code (above) differs from yours is that I chose not to make my Widget's 'member' member private. Perhaps this is the source of the error, but regardless, I do not want my Widget members to be private.

To patch the glitch, I just changed the name of the method to xclone() but used the same source code. However, it doesn't work! Observe:

Widget a = new Widget(5);
Widget b = a.xclone();
a.member = 20;

If I print Widget b's 'member' to screen, it stores the value as 20! The clone function has failed its purpose.


*pouting*

Share this post


Link to post
Share on other sites
Quote:
Original post by hisDudeness
\Now the only way my source code (above) differs from yours is that I chose not to make my Widget's 'member' member private.


You also didn't choose to make the clone() method public (the default is not the same thing as public). :)

Quote:
To patch the glitch, I just changed the name of the method to xclone() but used the same source code. However, it doesn't work! Observe:

Widget a = new Widget(5);
Widget b = a.xclone();
a.member = 20;

If I print Widget b's 'member' to screen, it stores the value as 20! The clone function has failed its purpose.


What you have should work. Make sure you recompiled properly.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!