Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can keyword be used as identifier?

939


Why do we override tostring method in java?

944


Can java arraylist hold different types?

1051


How do you remove duplicates in java?

997


Name few "optional" classes introduced with java 8 ?

1111


Write a program to check for a prime number in java?

1029


Can you sort a list in java?

965


What methods are used to get and set the text label displayed by a button object?

994


What are the 4 versions of java?

1149


How we can run a jar file through command prompt in java?

951


Which collection is ordered in java?

959


What does it mean that strings are immutable?

1113


What are the difference between string, string builder, and string buffer in java?

1107


What does flagged out mean?

1041


Under what conditions is an object’s finalize() method invoked by the garbage collector?

1020