How to make a class immutable?




Answers were Sorted based on User's Feedback



How to make a class immutable?..

Answer / kanchan

To make a class immutable,
1. Don't provide any methods that modify the object.
2. Ensure that no methods may be overridden. Mark the class
as final. Make all fields final and private.

Is This Answer Correct ?    63 Yes 1 No

How to make a class immutable?..

Answer / cm

1. Mark the class as 'final' to avoid any
subclassing/overriding
2. Make the fields as 'private' & 'final' to avoid
reassining in any way after creation of the object by
passing params via contructor

Is This Answer Correct ?    17 Yes 3 No

How to make a class immutable?..

Answer / tapan k dinda

To make class immutable you need to follow steps below:-
1. class should be final(strong Immutability) or all
methods final(weak Immutability)
2. all fields should be private
3. no setter/mutator should be provided
4. make deep copies of mutable data, if any

any confusion plz mention in your next post.

Is This Answer Correct ?    10 Yes 2 No




How to make a class immutable?..

Answer / raga

While creating class objects of which will be immutable. The following
things should be kept in mind:
1. Class should be made final so that no class can extend it.
2. Access modifiers of the instance variables must be private so that no
object can have access to it.
3. There should not be any public set method which can change the state of
the object.

Is This Answer Correct ?    1 Yes 0 No

How to make a class immutable?..

Answer / venkata siva reddy

1.Make the class as final.
2.Make all the properties (variables) as private and final
3.Provide only getter methods for variables.

Is This Answer Correct ?    1 Yes 0 No

How to make a class immutable?..

Answer / 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

How to make a class immutable?..

Answer / khizar khan

1) Make a class fianl like final class A{}

2) Then use the property as private and final
ex:
final class A{
private final int salary;

}
3)Now make a Constructor to initilize the values
ex:
A(int a)
{ this .salary=a;
}
4)Now use the gtter mathod to acces the values
ex:
final class A{
private final int salary ;
A( int a)
{
this.salary=a;

}
public int getSalary()
{
return salary;

}

}

Is This Answer Correct ?    0 Yes 0 No

How to make a class immutable?..

Answer / ravi jain

in above ans

marking the all methods as final gives no mean as
class is final.

for methods point of view do not provide any
setter/mutator methods in class.

Is This Answer Correct ?    2 Yes 3 No

How to make a class immutable?..

Answer / sudhakar sahoo

1.Mark the class as final to avoid subclass, So subclass
can't change any thing in super class.
2. When ever you are chaning any property of that class
make a new instance of that calss.
e.g public modify(Object obj0)
{
ClassName a =new ClassName ();
a.modify()// modification logioc
}

Is This Answer Correct ?    3 Yes 5 No

How to make a class immutable?..

Answer / rashmin

To make a class immutable follow the below steips

1. Make the class Final.
2. Make the instance variables private and final.
3. Make the methods in the class also final.

By doing the above 3 we can create a fully immutable class.

Is This Answer Correct ?    4 Yes 6 No

Post New Answer



More Core Java Interview Questions

What is jpa specification?

0 Answers  


What is a parent class in java?

0 Answers  


What’s the difference between unit, integration and functional testing?

0 Answers  


Will minecraft java be discontinued?

0 Answers  


what are the different access specifiers that can be used by interfaces and abstract classes? can anyone give me detailed description on this

11 Answers   L&T, IBM,






Is arraylist an object in java?

0 Answers  


What is ternary operator in java?

0 Answers  


What is a substitution variable?

0 Answers  


How does remove work in java?

0 Answers  


Differences between external iteration and internal iteration?

0 Answers  


What is the base class of all exception classes in java?

0 Answers  


How big is a boolean?

0 Answers  






Categories