0

Lets say I have list of Scenarios in a Game, and lists of Players. Each player plays some scenario from the Scenario list in Game. I don't want to have copy of Scenario in Player, I want to have something like pointer to the list in Game object, so that if any scenario is edited, it is also edited in Player list.

Game
Scenarios : list

Player
Scenarios : list -> each scenario here is a reference to scenario in Game

Now It would be simple in language supporting pointers, but Java is supposedly pass-by-value. At the same time, I know there are hacks using passing values inside tables etc. Is it possible to do this or not?

4 Answers 4

1

What have you tried? If you would have tried putting the reference into separate lists you would have known that they are the same reference. The term "pass-by-value" is tricky here because you will get a reference to the same object in your methods for example but java passes around copies of references. So basically java passes around values of references.

If you put the same reference in all your lists the changes to the original will be reflected in them.

SomeObject o = new SomeObject();

list0.add(o);
list1.add(o);
list2.add(o);

o.setSomeValue(true);

// all lists will "see" the changes

list0.get(0).equals(o); // assuming list0 only contains o -> this is true

As a rule of thumb: if you don't mess (swap, assign, etc.) with references and only call methods on the objects the references point to you'll be fine.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm currently planning a bigger (for me) project, and was wondering whether this is possible or if I should try to implement it differently. Also, I would try to get the someObject from list0, instead of creating one object and then sharing it, would it make a difference?
1

In your case, if Player object has a Scenario object, not the whole scenario will be copied (or cloned), only the reference of Scenario will be copied. And you can modify the Scenario either through reference by Player or through the List; as it is one object many references.

Comments

1

1st way

I would use interface in your case. Something like:

public class Scenario implements  ScenarioItf{/**/}

And the Player will store only ScenarioItf.

2nd way

To store Scenarios list into Factory class:

public class ScenariosFactory {

    private static List<Scenarios> mScenarios = null;


    public static void create(List<Scenarios> scenarios) {
        mScenarios = scenarios;
    }


    public static List<Scenarios> getFactory(){
        if ( mScenarios == null )
            throw new NullPointerException( "...null..." );
        return mScenarios;
    }
}

So both classes Player and Game can use the same source.

Comments

0

Java is pass-by-reference, so if you have a Scenario object in the list AND in the Player object, they will both be a pointer to the actual Scenario object in memory.

Edit: Apparently, it's sort of by-reference but technically not really. As long as you don't go scenario = new Scenario() at either end, it will be the same object.

6 Comments

It's Pass-by-value, but the value is a reference (so it's rather Pass-by-value-of-reference). But in this case the effect is what you described.
This is false. Check out my explanation in my answer.
@RomanVottner If you change the state (aka modify its members) of an object passed as a parameter, you will see the effect in the invoking routine. But if you bind the reference to another object, it will have no effect in the invoking method. A famous example for the difference between Java and languages that support pass-by-reference is that implementing a generic swap()-method isn't possible in Java (you should find lots of info on this via google).
I've always HATED the statement, "Java is pass by value." If it was, then you could not see modifications to an object in two different scopes. With the exception of primitives, the "value" being passed is a reference to an object, i.e. passing the reference. So I consider it pass by reference with an asterisk for primitives.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.