Example:Immutable Class
Immutable class is thread safe
Imuutable class objects cannot be modified
Examples :
//make ur class final
public final class MyImmutable {
//make ur instance variables final
final String s="Anjana";
private String s2=null;
//access the private via constructors--to modify
public MyImmutable(String s2) {
super();
this.s2 = s2;
}
public static void main(String args[]){
MyImmutable myimmutable=new MyImmutable("dff");
}
}
Imuutable class objects cannot be modified
Examples :
- Integer
- String
//make ur class final
public final class MyImmutable {
//make ur instance variables final
final String s="Anjana";
private String s2=null;
//access the private via constructors--to modify
public MyImmutable(String s2) {
super();
this.s2 = s2;
}
public static void main(String args[]){
MyImmutable myimmutable=new MyImmutable("dff");
}
}
Comments
Post a Comment