Object mutability
-
How would one mutate an object that is final? By referencing the object and then doing a mutate() operation on it? e.g.:
final Object o = new Object();
o.mutate();I don't understand. If it was declared final, then how come you are allowed to mutate it (or is that not true)? How would one make the Object immutable then? Meaning: not mutable in any way, not even by reference. How do I make sure a reference cannot modify the object it refers to? So that MyObject o = new Object(); o.modify(); is not possible? I also read on Wikipedia the following example:
final Position pos = new Position(); //pos is immutable
members: pos.x, pos.y, pos.z are mutable still, unless they are declared final. So if I am understanding this correctly, in order to make an object/class completely immutable, everything in it including its reference has to be declared final? e.g.:
final Position pos = new Position();
class Position {
final int x;
final int y;
final int z;public Position(){
x=1;
y=2;
z=3;
}final void doA(){}
final void doB(){}
} -
How would one mutate an object that is final? By referencing the object and then doing a mutate() operation on it? e.g.:
final Object o = new Object();
o.mutate();I don't understand. If it was declared final, then how come you are allowed to mutate it (or is that not true)? How would one make the Object immutable then? Meaning: not mutable in any way, not even by reference. How do I make sure a reference cannot modify the object it refers to? So that MyObject o = new Object(); o.modify(); is not possible? I also read on Wikipedia the following example:
final Position pos = new Position(); //pos is immutable
members: pos.x, pos.y, pos.z are mutable still, unless they are declared final. So if I am understanding this correctly, in order to make an object/class completely immutable, everything in it including its reference has to be declared final? e.g.:
final Position pos = new Position();
class Position {
final int x;
final int y;
final int z;public Position(){
x=1;
y=2;
z=3;
}final void doA(){}
final void doB(){}
} -
See Java theory and practice: To mutate or not to mutate?[^].
Use the best guess
Referring to that article, Listing 1: Q.: "Because Date is mutable, the scheduleTask method must be careful to defensively copy the date parameter (perhaps through clone()) into its internal data structure. Otherwise, task1 and task2 might both execute tomorrow, which is not what was desired. Worse, the internal data structure used by the task scheduler could become corrupt." A few questions on this: 1) What is 'copying defensively'? 2) What do they mean by 'internal data structure'? 3) How could task1 and task2 execute tomorrow if the code is to be executed today? Then how can new Date() be tomorrow? I don't understand. 4) How would the internal data structure become 'corrupt'? What is 'corrupt' in this context? Containing inconsistent values?
-
Referring to that article, Listing 1: Q.: "Because Date is mutable, the scheduleTask method must be careful to defensively copy the date parameter (perhaps through clone()) into its internal data structure. Otherwise, task1 and task2 might both execute tomorrow, which is not what was desired. Worse, the internal data structure used by the task scheduler could become corrupt." A few questions on this: 1) What is 'copying defensively'? 2) What do they mean by 'internal data structure'? 3) How could task1 and task2 execute tomorrow if the code is to be executed today? Then how can new Date() be tomorrow? I don't understand. 4) How would the internal data structure become 'corrupt'? What is 'corrupt' in this context? Containing inconsistent values?