This discussion is archived
4 Replies Latest reply: Oct 5, 2012 7:44 AM by 965972 RSS

Questions about passing by reference

965972 Newbie
Currently Being Moderated
Greetings,

I've been reading more deeply into Java's handling of parameter passing and while I'm getting what the reputable articles and forums are saying about everything being a pass-by-value, I am having trouble with a test program I'm writing for work to simulate electrical switching logic.

In brief, to be generic, suppose I have two objects, A and B, of the same type. Forgetting everything else for the moment, they each hold a value (double). I initialize the two objects to hold different values so I can recognize them with print statements. (I may not eventually need to do that, and just change the value of a single instance, A, and get rid of B entirely, but I'm still working my way through the best approach on this.)

I have a third object which contains a reference to the type that A and B are (call it C). I initialize it to point to A, then later, I reset its reference to point to B. Print statements again confirm the before-and-after behavior is different, i.e. that C's reference does indeed later point to B instead of A, as desired.

Now here's the kicker: I have a fourth object which is initialized to point to C (call it D), and I am making the assumption that even though Java passes parameters by value (making a copy of the reference [pointer] value), the copy should still point to C, which near the end of the program, in theory should be pointing to B and its contained value. I'm not trying to change it; in fact, I want it to remain a "hard linkage" - that is, D always points to C, but whatever C points to can change as desired.

But I'm not seeing that.

I was wondering if anyone could give me an idea of what might be going on here.

Thanks,
Mike

P.S. Here's the more specific version, without giving too much away (toString()s are implemented, I just haven't shown them):

public class ElectricalSignal {
private double value = 0.0; // Assume this is a voltage

public void setValue(final double val) {
value = val; // Set a value during runtime as desired
}

public double getValue() {
return value; // Get the value
}

public ElectricalSignal(final double val) {
// Just init the value
value = val;
}
}

public class ElectricalBus {
private ElectricalSignal signal; // The signal that the bus will carry

public void setSignal(final ElectricalSignal sig) {
signal = sig; // Change the signal we're pointing to during runtime (i.e. switching)
}

public ElectricalSignal getSignal() {
return signal; // Get a reference to the signal if necessary
}

public ElectricalBus(final ElectricalSignal sig) {
signal = sig; // Initialize the bus to hold a certain signal
}
}

public class Relay() {
// Ignore other stuff that relays do for this example
...
ElectricalSignal input = null; // We have an input to the relay

public Relay(final ElectricalSignal signal) {
input = signal; // Set the input to be the bus of interest's signal on construction
// Ignore other operations
...
}
}

// In application's main()

ElectricalSignal A = new ElectricalSignal(0.0); // Establish a 0VDC signal
ElectricalSignal B = new ElectricalSignal(28.0); // Establish a 28VDC signal
...
ElectricalBus C = new ElectricalSignal(A); // Set the bus to carry the 0VDC signal at the outset
...
Relay D = new Relay(C.getSignal()); // Create a relay and point it to the signal - this linkage should never change

// *** NOTE: When I first wrote this code, I was going off memory, I had the relay taking in a reference to a bus
// object and not a signal object. My real code uses a signal object, and that might be the problem.

D.toString(); // Verify the situation - this is correct info
...
C.setSignal(B); // Now switch the signal being carried by the bus
D.toString(); // Whoops! We're still pointing to A, even though I assert it should have changed to B
...
  • 1. Re: Questions about passing by reference
    jtahlborn Expert
    Currently Being Moderated
    The return value from the method call "C.getSignal()" is a reference to a Signal (you are copying the reference currently held by C and returning it). It is not a reference to the reference currently held by C. if you understand C (the computer language) programming, the return value is a "Foo*", not a "Foo**". in order to do what you want, D needs to keep a reference to C (your Relay should reference a Bus, not a Signal)..
  • 2. Re: Questions about passing by reference
    rp0428 Guru
    Currently Being Moderated
    >
    D.toString(); // Whoops! We're still pointing to A, even though I assert it should have changed to B
    >
    Why would it change to B? You set D to what C 'used to' be using this code
    Relay D = new Relay(C.getSignal()); // Create a relay and point it to the signal - this linkage should never change
    And the constructor of D uses 'final' meaning it can't be changed.
    public Relay(final ElectricalSignal signal) {
    input = signal; // Set the input to be the bus of interest's signal on construction
    So you load D with something that can't change and then you wonder why it didn't change?
  • 3. Re: Questions about passing by reference
    doremifasollatido Journeyer
    Currently Being Moderated
    rp0428 wrote:
    And the constructor of D uses 'final' meaning it can't be changed.
    public Relay(final ElectricalSignal signal) {
    input = signal; // Set the input to be the bus of interest's signal on construction
    So you load D with something that can't change and then you wonder why it didn't change?
    That particular 'final' just means you can't do this:
    public Relay(final ElectricalSignal signal) {
       signal = null; // Illegal--can't assign to a final variable.
    }
    It doesn't mean you can't change what D points to using:
    D.setSignal(null);
    D.setSignal(new ElectricalSignal(5.0));
    assuming that a setSignal method exists in the Relay class.
  • 4. Re: Questions about passing by reference
    965972 Newbie
    Currently Being Moderated
    Yeah, the guy before you got the final modifier wrong. I actually spotted the error as I was writing the post and have since corrected the issue. Everything works like I wanted it to!

    Mike

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points