Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by HemoGloben Apr 6, 2005 at 9:50 PM 7 replies 1.7k views
Original Post
HemoGloben
HemoGloben
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;}
justo
justo
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.
capn_midnight
capn_midnight
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]
HemoGloben
HemoGloben
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;}
Malone1234
Malone1234
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.
Alpha_ProgDes
Alpha_ProgDes
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.  
SamLowry
SamLowry
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.