Last active
April 5, 2024 22:05
-
-
Save ClementGre/5481dcd27164313d89fd1f32e848a819 to your computer and use it in GitHub Desktop.
Understanding immutable and mutable objects, and how using "=" is losing the reference from the initial object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.clementgre; | |
public class Main { | |
public static void main(String[] args) { | |
MutablePerson friend = new MutablePerson(null, "Friend man", 20); | |
MutablePerson person = new MutablePerson(friend, "Person test", 19); | |
editMutablePerson(person); | |
System.out.println(person); | |
int i = 10; | |
editInt(i); | |
System.out.println(i); | |
String str = "this is the original str"; | |
editString(str); | |
System.out.println(str); | |
} | |
public static void editMutablePerson(MutablePerson person){ | |
// This just edit the person without re-defining it | |
person.setAge(30); | |
// By using "=", we are re-defining the value of the var person, so there is no link anymore with the main() person | |
person = new MutablePerson(null, "Doing this is useless, the main() person var is not edited", 17); | |
// Doing person = mew ... | |
// Equals MutablePerson newPerson = new ... | |
// (loosing the old variable person) | |
} | |
public static void editInt(int i){ | |
// To edit i, we are obliged to use "=", re-defining the var, because all primitives types are immutable. | |
i = 3; | |
} | |
public static void editString(String str){ | |
// String (as Integer and a lot of others classes), is immutable so we get a new object for all methods of | |
// String. There is no way to edit the string, we have to create a new one; | |
String added = str.concat(" <- this is our str"); | |
str = "new str"; | |
} | |
// You can edit the fields of this object after it was initialised, by calling the setters methods. | |
public static class MutablePerson{ | |
private MutablePerson friend; | |
private String name; | |
private int age; | |
public MutablePerson(MutablePerson friend, String name, int age){ | |
this.friend = friend; | |
this.name = name; | |
this.age = age; | |
} | |
public MutablePerson getFriend(){ | |
return friend; | |
} | |
public void setFriend(MutablePerson friend){ | |
this.friend = friend; | |
} | |
public String getName(){ | |
return name; | |
} | |
public void setName(String name){ | |
this.name = name; | |
} | |
public int getAge(){ | |
return age; | |
} | |
public void setAge(int age){ | |
this.age = age; | |
} | |
@Override | |
public String toString(){ | |
return "MutablePerson{" + | |
"friend=" + friend + | |
", name='" + name + '\'' + | |
", age=" + age + | |
'}'; | |
} | |
} | |
// Creating an immutable class as an example : this class can't be edited after it was initialized | |
// To edit one of it's fields, we have to create a new one, this is the working of Strings, and all primitives types | |
public static final class ImmutablePerson{ | |
// The fields have to be Immutable too, or else, this class would not be fully immutable | |
private final ImmutablePerson friend; | |
private final String name; | |
private final int age; | |
// Private constructor so no class can extends it, adding mutable fields | |
private ImmutablePerson(ImutablePerson friend, String name, int age){ | |
this.friend = friend; | |
this.name = name; | |
this.age = age; | |
} | |
// No setters so the fields can't be edited (Moreover, the fields are marked as final) | |
public ImutablePerson getFriend(){ | |
return friend; | |
} | |
public String getName(){ | |
return name; | |
} | |
public int getAge(){ | |
return age; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment