Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,4 @@
|
|||
final int immutableInt = 4;
|
||||
int mutableInt = 4;
|
||||
mutableInt = 6; //this is fine
|
||||
immutableInt = 6; //this is an error
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
final String immutableString = "test";
|
||||
immutableString = new String("anotherTest"); //this is an error
|
||||
final StringBuffer immutableBuffer = new StringBuffer();
|
||||
immutableBuffer.append("a"); //this is fine and it changes the state of the object
|
||||
immutableBuffer = new StringBuffer("a"); //this is an error
|
||||
25
Task/Enforced-immutability/Java/enforced-immutability-3.java
Normal file
25
Task/Enforced-immutability/Java/enforced-immutability-3.java
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
public class Immute{
|
||||
private final int num;
|
||||
private final String word;
|
||||
private final StringBuffer buff; //still mutable inside this class, but there is no access outside this class
|
||||
|
||||
public Immute(int num){
|
||||
this.num = num;
|
||||
word = num + "";
|
||||
buff = new StringBuffer("test" + word);
|
||||
}
|
||||
|
||||
public int getNum(){
|
||||
return num;
|
||||
}
|
||||
|
||||
public String getWord(){
|
||||
return word; //String objects are immutable so passing the object back directly won't harm anything
|
||||
}
|
||||
|
||||
public StringBuffer getBuff(){
|
||||
return new StringBuffer(buff);
|
||||
//using "return buff" here compromises immutability, but copying the object via the constructor makes it ok
|
||||
}
|
||||
//no "set" methods are given
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue