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
#are instances updated on their own?
1 messages · Page 1 of 1 (latest)
Detected code, here are some useful tools:
Formatted code
public final File onGoingTasksFile;
public YamlConfiguration onGoingTasksConfig;
onGoingTasksConfig = YamlConfiguration.loadConfiguration(onGoingTasksFile);
MineDiamonds = new MineDiamonds(this , onGoingTasksConfig, onGoingTasksFile);
<@&987246399047479336> please have a look, thanks.
thats expected behavior. java is pass-by-value
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.
I tried the same thing with hashmaps and they worked just fine, updated whenever i changed them in another class. Are they an exception ?
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 ?
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
Hmm, so it's working that way because the hashmap is a mutable object and the file object just tells ths path of a file which itself is immutable, but it's contents gets updated in all the classes whenever the actual file has been updated, hence updating it in other classes
I think if you imagine it like that it could work, but the File object just sort of "refers" to the actual file on the hard drive/storage media, so if the data on the hard drive changes, when your code tries to access it from the File object, your code will see those changes.
Exactly what I wanted to confirm, thanks
No problem.
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.
thanks for the deeper explanation