How to make a class immutable?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
How does finally block differ from finalize() method?
How are variables stored in memory?
What is the use of :: in java?
What is the SimpleTimeZone class?
Can we have return statement in finally clause? What will happen?
What is matcher in java?
What is the purpose of encapsulation?
What is the locale class in java programming?
How many bits is a boolean?
can we write two same methods in outer class and innerclass.
How can you add and remove nodes in jtree?
ublic class Java_Coding_Samples { public static void JavaHungr(NumberFormatException ae){ System.out.println("integer"); } public static void JavaHungry(Exception e){ System.out.println("string"); } public static void JavaHungry(ArithmeticException ae){ System.out.println("object"); } public static void main(String[] args) { JavaHungry(null); }