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

What is the properties class in java programming?

560


What is length in java?

581


EDS (Electronic Data Systems India Pvt Ltd) at Chennai on 16-12-2006.

8634


How does sublist works in java?

555


Difference between association, composition and aggregation?

567






What does this mean java?

568


Does java support function overloading, pointers, structures, unions or linked lists?

617


What are the main features of java?

536


Is java owned by oracle?

530


What are memory tables?

616


what is meant by Byte code concept in Java?

605


Is it possible to write a regular expression to check if string is a number?

550


If an object reference is set to null, will the garbage collector immediately free the memory held by that object?

645


Can you pass functions in java?

578


Why do you canvas?

620