How to make a class immutable?

Answer Posted / venu gopala reddy

a better approach. Make the immutable class itself final. Hence cannot make any sub classes, so no question of over ridding.
write code for user immutable class:-


final class ImmutableVenu{

private final int count;

private String phno="9742108000";

public ImmutableVenu(int paramCount,String paramPhno){

count=paramCount;

phno=paramPhno;
}

public int getCount(){
return count;
}

public String getString(){
return paramPhno;
}
}

public static void main(String ar[])
{

ImmutableVenu immu=new ImmutabeVenu();
s.o.p(immu.getCount());
s.o.p(immu.getPhno());
}

Is This Answer Correct ?    1 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why there are some null interface in java? What does it mean?

630


What is the constructor?

569


What is meant by call by reference?

494


What is sizeof in java?

542


What do you mean by data type?

535






What is a blocking method in Java?

589


What are singleton services?

479


What is a java list?

567


What is meant by method?

561


Can you inherit from an abstract class java?

517


What is the purpose of declaring a variable as final?

474


What is increment in java?

511


Can we override private methods?

529


What is collection class in java?

529


How can u increase the heap size in the memory?

538