can any one send me the example program of immutable class?
Answer Posted / jagannath
public class ImmutableClass {
int i;
public ImmutableClass(int i)
{
this.i=i;
}
public int getI()
{
return i;
}
public ImmutableClass setI(int i)
{
if(i==this.i)
{
return this;
}
else
return new ImmutableClass(i);
}
public static void main(String args[])
{
ImmutableClass ic = new ImmutableClass(5);
ic.getI();
System.out.println(ic);
ic = ic.setI(10);
System.out.println(ic);
}
}
// If you pass 5 as the value in setter method, you will see
same address. It means whenever you are trying to change the
value of variable, a new object is created and returned. So
your object is immutable.
| Is This Answer Correct ? | 5 Yes | 2 No |
Post New Answer View All Answers
Name component subclasses that support painting in java programming?
What is the program development process?
What is java used for?
What is the difference between sop and work instruction?
What Is Query Throttling in java?
When do we need to use internal iteration? When do we need to use external iteration?
What is the difference in between cpp and java? Can u explain in detail?
What is another word for methodology?
What is object data type?
Which types of exceptions are caught at compile time?
What is a return in java?
What does localhost mean?
What is output buffer?
How do you override a private method in java?
What will be the default values of all the elements of an array defined as an instance variable?