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

Which of these methods belong to Thread & Object class? join, yield, sleep, wait, notify

3 Answers   Ericsson,


Is java 1.7 the same as java 7?

0 Answers  


Difference between local and global transaction ?

1 Answers   Accenture, iFlex,


Describe the syntax of multiple inheritance? When do we use such an inheritance?

0 Answers   Fidelity,


Explain about serializable interface in java?

0 Answers  






What do the thread?class methods run() and start() do?

0 Answers  


How do you ensure that n threads can access n resources without deadlock?

0 Answers  


real time example for deadlock,starvation,livelock

5 Answers  


What is access modifiers?

1 Answers   Cap Gemini,


How can we create objects if we make the constructor private ?

0 Answers  


What is Session and cookies?Explain in detail with an example?

4 Answers   Accenture, CTS,


Name container classes in java programming?

0 Answers  


Categories