Answer Posted / java
1.Make the class as final
2.Make the data members as private
3.write the public getter methods
Ex:
public class ClassImmutable {
public static void main(String[] args) {
ImmutableTest it=new ImmutableTest(10,"Count");
System.out.println(it.getString());
System.out.println(it.getValue());
}
}
final class ImmutableTest{
private int i;
private String str;
public ImmutableTest(int i,String str){
this.i=i;
this.str=str;
}
public int getValue(){
return i;
}
public String getString(){
return str;
}
}
| Is This Answer Correct ? | 0 Yes | 2 No |
Post New Answer View All Answers
What is native code?
What is java developer skills?
What java is used for?
What is a ternary operator in java? What is an interface?
What is a arraylist in java?
What is difference between final and immutable?
Can java run on google chrome?
What is a function in programming?
Can you tell me range of byte?
What is nullpointerexception?
What are streams?
What is null in java?
What is an inner class in java?
Can we extend singleton class?
What is unicode datatype?