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 a qualifier in a sentence?
Is java type safe?
Where pragma is used?
Why we use protected in java?
Can we overload the main() method?
Why java is a platform independent? Explain
What is memory leak and how does java handle it?
What is the purpose of static methods and static variables?
What is object-oriented programming?
What is private public protected in java?
What are alternatives to java serialization?
Can we override private constructor in java?
What is a byte string?
What is the default value of the local variables?
What does it mean that a class or member is final?