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
Why is java called java?
How do you implement tree mirroring in java?
What is javac_g?
what are the states associated in the thread? : Java thread
What is array in java?
What is nan in java?
What is the purpose of the file class in java programming?
What is length in java?
What is advantage of java?
What is the purpose of using break in each case of switch statement?
Is finalize() similar to a destructor?
How can we create a synchronized collection from given collection?
Explain about procedural programming language or structured programming language and its features?
What environment variables do I need to set on my machine in order to be able to run java programs?
What do you mean by an object in java?