#are instances updated on their own?

1 messages · Page 1 of 1 (latest)

frozen violet
#
    public 
YamlConfiguration onGoingTasksConfig;

onGoingTasksConfig = YamlConfiguration.loadConfiguration(onGoingTasksFile);
        MineDiamonds = new MineDiamonds(this, onGoingTasksConfig, onGoingTasksFile);```

im passing the instance of onGoingTasksConfig here but whenever i update it, its only updated in the main class but not in the class where i am passing the instance to. is this what its supposed to do or I am doing something wrong here
tacit robinBOT
#

<@&987246399047479336> please have a look, thanks.

dark field
lost solar
#

You can think of pass by value as no different than two local variables.

   int a = 10;
   int b = a;
   b = 20;

You wouldn't expect that a is now 20 just because a new value was assigned to b.

Passing a value to a method is no different, whether the type of that argument is a primitive or a reference-type. Any assignment they make to their copy doesn't affect yours.

However. With reference types, your reference and their copy both refer to the same object - changes to the object can be observed from both references. But if one or both of you reassign your variable to refer to a different object, that is not observable (without communicating that via a mechanism such as returning a value from the method which received a copy of your reference.

frozen violet
frozen violet
#

here's what I have understood till now

If I pass a File object or a hashmap then change it in either of the classes, it would reflect the change in the other one too. Whereas if I pass a yaml Configuration it won't work like that, I would have to update it manually using the file

#

Is this right ?

high ice
#

So, it depends. first of all, the pass by value counts for numbers and objects, but you can change the underlying objects IF they're mutable

frozen violet
noble meteor
frozen violet
noble meteor
#

No problem.

lost solar
#

In this respect, of passing a reference to a method, HashMap and File work exactly the same.

  • The reference is copied in the method parameter
  • The method can access fields and methods on the object referenced by the parameter
  • Assigning a new value to the parameter inside the method has no effect on the caller.

The difference is that that HashMap has methods that allow you to alter the state in the HashMap object (eg .clear(), put(...), etc), but File (for observable changes) does not.

frozen violet