top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Please explain the difference between Immutable and Singleton in Java?

+2 votes
2,786 views

Also it would be helpful if can tell when to use immutable class and when singleton?

posted Aug 12, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

An immutable object is initialized by its constructor only, while a singleton is instantiated by a static method.

1.  A set of functions (or static methods) which manipulate some shared mutable state constitute a singleton.

2.  If a singleton A provides a reference to mutable object B, then B is a singleton as well.

3.  This implies that every mutable member of a singleton collection is itself a singleton.

4.  A transitively immutable object is NOT a singleton even if globally accessible. It is a constant.

5.  A stand-alone function that does not access any singletons is NOT itself a singleton, assuming that code is immutable.
answer Aug 14, 2015 by Karthick.c
...