[java] Java reference...help...

Started by
7 comments, last by GameDev.net 19 years ago
I'm trying to understand how Java handles references and such, and I don't quite get it. What I want to do is have two objects 'linked' so that I can just change one, and the other changes automatically. I was thinking of something like this:

class NewObj
{
    public Object obj;
    public NewObj(Object tObj)
    {
         obj=tObj;
    }
}


class OldObj
{
    public Object obj;
    public OldObj(Object tObj)
    {
         obj=tObj;
    }
}


class Main
{
     public static void main(String args[])
     {
          Object obj = new Object();
          NewObj nObj = new NewObj(obj);
          OldObj oObj = new OldObj(obj);
     }
}

If I did something like that, and I changed the value of 'obj' would the values of nObj, and oObj change accordingly? Or how else would I accomplish something like this? PS:sorry for the terrible code, just coded it as an example, best I could think of righ tnow.
if(this.post == SATISFYING){money.send(1.00,&HemoGloben);return thanks;}
Advertisement
yep, you got it right. all you are doing when you pass NewObject and OldObject a reference to obj is passing them a reference to obj, not the object itself.

in other words, nObj.obj, oObj.obj and obj are all references to the same object by the end of your main method...doing anything to any of them would be reflected through all of them...since they all point back to the same, singular object.
if you are at all familiar with C/C++ pointers and references, it's actually more accurate to think of Java's "references" as "pointers".

there is a post on this in the FAQ, in fact I wrote it.

[Edited by - capn_midnight on April 7, 2005 12:53:11 AM]

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Thanks guys.

I got the whole Java reference is pretty much equal to a C++ pointer, but I was searching on google, and people seemed to be making such a big deal over something so simple that I thought it had to be more complicated than that...guess not.
if(this.post == SATISFYING){money.send(1.00,&HemoGloben);return thanks;}
It's really not a big deal but it tends to become a headache because in Java you cannot have a "reference" (pointer) to a variable of a primitive type.
does Java have copy constructors or something similar.

for instance:
String word = new String("word");String phrase = new String();//what i'm looking for here is phrase and word to have the same value,//but not point to the same memoryphrase = word;

Beginner in Game Development?  Read here. And read here.

 

You're looking for Object.clone()

Object provides a default implementation (shallow copy, IIRC) but classes can override it to provide custom copying.
Technically, clone() does not fit his needs.

String phrase1 = new String();String phrase2 = phrase1;String word    = "Word";phrase1 = word.clone();


AFAIK, using a copy constructor, both phrase1 and phrase2 should be equal to "Word", which is not the case using clone().


Personally, I stay as far away from clone as possible. The design is somewhat... strange (the empty Cloneable interface, and a protected clone() method in Object, which a class implementing Cloneable must then declare public). Bloch advises "you are probably better off providing an alternative means of object copying", which I can only agree with.


To answer to Mr. ProgDes's question: to my knowledge, there is no such "syntactic sugar" present in Java, you'll have to define a separate method.
...However, there's no real reason to not want two Strings to point to the same place in memory, and no reason to believe the JVM won't analyse your code and re-use the same object regardless. Strings are a bad example! :-)

Basically, the person who wrote the object you're using has to provide a way for you to duplicate it. Whether this is via a factory method, via Object.clone(), or a copy constructor-alike is down to applicability. If they haven't provided any way for you to copy the object, then they don't want you to do it and there's no way to force it.

Note that Object.clone() is protected, and even if accessible won't work unless the object is Cloneable. And yes, it's a shallow copy.

--cfmdobbie

This topic is closed to new replies.

Advertisement