Closed Thread Icon

Topic awaiting preservation: Java pass by reference? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=13048" title="Pages that link to Topic awaiting preservation: Java pass by reference? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Java pass by reference? <span class="small">(Page 1 of 1)</span>\

 
bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 02-06-2004 00:14

Hey all, I'm working on my first real java project and I'm running into a question.

I've got a class call it the "master class" who instantiates an object within it, something like so:

public class masterClass {
masterClass() {
Obj1 childObj1 = new Obj1();
}
}


Now I want to create a couple more different types of Objects we'll call them objects 2 and 3. And I want them to be able to access the data of Obj1 ( Object 1 in this case is a "map" object for a game so it is mostly data and some common methods on that data. ) so now I have something like so:

public class masterClass {
masterClass() {
Obj1 childObj1 = new Obj1();
Obj2 childObj2 = new Obj2( childObj1 );
Obj3 childObj3 = new Obj3( childObj1 );
}
}

where child object 2 and 3 looks something like so

public class Obj2 {
private Obj1 obj;
Obj2( Obj1 obj ) {
this.obj = obj;
}
}


Now my question is, I'm not in this case actually copying and creating new Obj1 objects each time I pass this Object into one of my Obj2 or Obj3 classes right? I was reading up on some info on java passing-by-reference or more precisely "Java is pass-by-value" but I just want to be sure.

Also is there a better way to do this?

much thanks.





.:[ Never resist a perfect moment ]:.

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 02-06-2004 15:29

Java is 'pass by value',
but I believe your problem stems from a misconception of what pass-by-reference and pass-by-value actually are.
Pass-By-Value: You pass in a value, which might be an object. Said value might be mutated in your method (ie. you call a method on an object changing it's state), but it's still going to be the same value you passed in. (minus any state changes within the value, of course)

Pass-By-Referenc. You pass a reference to a value in. You could get a reference to a different value out. Or to the same one (mutated or not). Or a null reference.

'Pass-By-Copy' - You get a copy of the value in your method. Whatever you do, the caller won't see it. Welcome to the world of PHP.

So long,
TP

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 02-07-2004 21:40

Someone correct me if I'm wrong about this.

code:
// create new instance of ExampleObject
thisObject = new ExampleObject();
thisObject.setValue(0);
thisObject.getvalue(); // returns 0



thisObject is not, in itself, an object. Instead, it is a reference to an object in memory.

code:
// copy the reference (but NOT making a copy of the object)
thatObject = thisObject;
thatObject.setValue(1);
thisObject.getValue(); // returns 1, not 0



Thus, thisObject and thatObject are actually just references to the same object. In theory, this should work...

code:
thatObject = new ExampleObject();
containingObject = new ContainingObject(thatObject);

// containingObject's constructor assigns the argument to a private variable --
// that is, stores the object reference (not the object itself) as a "data member"
containingObject.setExampleObjectValue(2);
thatObject.getValue(); // should return 2



In many cases, you can consider the variable and the object as the same; but when passing objects around, remember that unless you specifically say "new Object()", you're passing a new reference (connection, handle, link, however you prefer to think about it), not a new instance of the object.

Cell 1250 :: alanmacdougall.com :: Illustrator tips

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 02-07-2004 22:10

this is not 100% correct:
You are passing around the object!
You're correct, it is not a copy that you pass around, but the object (the mechanism is implemented as a reference internally), but the important part is: You don't see the reference, and can not fiddle with it.

This is the the difference betwen pass-by-value (which is what perfect thunder described) and pass-by-reference, where the reference is passed around, and could be changed to point to a different object.

« BackwardsOnwards »

Show Forum Drop Down Menu