What is immutable class? how to make a Class explicitly
"Immutable"?Wap to make a class explicitly immutable.

Answers were Sorted based on User's Feedback



What is immutable class? how to make a Class explicitly "Immutable"?Wap to make a class e..

Answer / rv.nandakishore

I think u know... String is not a data type.. it is a
class...right..

By default the String class is a immutable class. That means
once we instantiate the object or assigning the value to
String is immutable. Once we can assign / instantiate the
String class we can't change in the future....


class StringDemo
{
public static void main(String[] args)
{
String s1="Nanda";
String s2="Kishore";
String s3=new String("Nanda Kishore");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}



U try any changes anything and print s1,s2 & s3.... If
changes then it is not immutable. If not it is immutable.


Is This Answer Correct ?    2 Yes 0 No

What is immutable class? how to make a Class explicitly "Immutable"?Wap to make a class e..

Answer / qamrun nisa

Immutable objects whose state (i.e. the object’s data) does
not change once it is instantiated (i.e. it becomes a
read-only object after instantiation). Immutable classes are
ideal for representing numbers (e.g. java.lang.Integer,
java.lang.Float, java.lang.BigDecimal etc are immutable
objects), enumerated types, colors (e.g. java.awt.Color is
an immutable object), short lived objects like events,
messages etc.

Writing an immutable class is generally easy but there can
be some tricky situations. Follow the following guidelines:
1. A class is declared final (i.e. final classes cannot be
extended).
public final class MyImmutable { … }
2. All its fields are final (final fields cannot be mutated
once assigned).
private final int[] myArray; //do not declare as -> private
final int[] myArray = null;
3. Do not provide any methods that can change the state of
the immutable object in any way – not just setXXX
methods, but any methods which can change the state.
4. The “this” reference is not allowed to escape during
construction from the immutable class and the immutable
class should have exclusive access to fields that contain
references to mutable objects like arrays, collections
and mutable classes like Date etc by:
• Declaring the mutable references as private.
• Not returning or exposing the mutable references to the
caller (this can be done by defensive copying)

Is This Answer Correct ?    1 Yes 0 No

What is immutable class? how to make a Class explicitly "Immutable"?Wap to make a class e..

Answer / qim2010

Immutable objects are those whose state (i.e. the object’s
data) does not change once it is instantiated (i.e. it becomes a
read-only object after instantiation). Immutable classes are
ideal for representing numbers (e.g. java.lang.Integer,
java.lang.Float, java.lang.BigDecimal etc are immutable
objects), enumerated types, colors (e.g. java.awt.Color is
an immutable object), short lived objects like events,
messages etc.

Writing an immutable class is generally easy but there can
be some tricky situations. Follow the following guidelines:
1. A class is declared final (i.e. final classes cannot be
extended).
public final class MyImmutable { … }
2. All its fields are final (final fields cannot be mutated
once assigned).
private final int[] myArray; //do not declare as -> private
final int[] myArray = null;
3. Do not provide any methods that can change the state of
the immutable object in any way – not just setXXX
methods, but any methods which can change the state.
4. The “this” reference is not allowed to escape during
construction from the immutable class and the immutable
class should have exclusive access to fields that contain
references to mutable objects like arrays, collections
and mutable classes like Date etc by:
• Declaring the mutable references as private.
• Not returning or exposing the mutable references to the caller
(this can be done by defensive copying)

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

What is variable length arguments in java?

0 Answers  


which method is used to know the status of the Thread?

9 Answers   Honeywell,


What is function and its uses?

0 Answers  


What restrictions are placed on method overloading in java programming?

0 Answers  


Is final static java?

0 Answers  






Explain about collection interface in java?

0 Answers  


How can we create a synchronized collection from given collection?

0 Answers  


Can we serialize arraylist in java?

0 Answers  


What is immutable state?

0 Answers  


When should you make a function static?

0 Answers  


What is type casting?

2 Answers  


What is meant by memory leak?

0 Answers  


Categories