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

Why charat is used in java?

0 Answers  


When will we use them?

0 Answers  


What is the difference between compile-time polymorphism and runtime polymorphism?

0 Answers  


What is a function easy definition?

0 Answers  


Adapter classes?

3 Answers  






Which collections are thread safe in java?

0 Answers  


Does it matter in what order catch statements for filenotfoundexception and ioexception are written?

0 Answers  


Describe the process as to how substring() methodology mechanisms in java.

0 Answers  


Can private method static?

0 Answers  


List implementations of list interface?

0 Answers  


What does flag mean in java?

0 Answers  


Is ruby built on java?

0 Answers  


Categories