object references

Started by
12 comments, last by Enigma 18 years, 3 months ago
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'??
Well I believe in God, and the only thing that scares me is Keyser Soze.
Advertisement
No. Objects are only by reference when used as a function argument.

Widget b = a; just creates a copy of a.
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
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.
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.
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.
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 widgetWidget b = a.clone();      // creates a new widget with same state as ab.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]
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*
Well I believe in God, and the only thing that scares me is Keyser Soze.
i may be wrong, but doesnt clone return an Object?
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.

This topic is closed to new replies.

Advertisement