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
Explain the importance of thread scheduler in java?
Explain the transient field modifier?
What is arraylist e in java?
List the features of java programming language.
What is incompatible types in java?
What are classloaders?
What does a void function return?
How to sort an array from smallest to largest java?
What is the primary benefit of encapsulation?
What does the exclamation mark mean in java?
How a variable is stored in memory?
Can you explain the final method modifier?
Can we use catch statement for checked exceptions when there is no chance of raising exception in our code?
What is the purpose of garbage collection in java?
What is the difference between scrollbar and scrollpane?