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 variable simple definition?
Why stringbuffer is faster than string?
What are the four corner stones of oop?
Is empty .java file name a valid source file name?
How do you implement polymorphism in our day to day life?
What is binary search in java?
How can an object be unreferenced?
What is the use of singleton class?
What is jagged array in java?
Can a static block throw exception?
What is entry set in java?
How to reverse string in java?
how to run ecllipse with jettyserver for windows environment using batch file
Tell me the latest versions in java related areas?
Explain how to force the garbage collection in java.