can any one send me the example program of immutable class?

Answer Posted / jinxuan

public class SingletonTest
{
public static void main(String[] args)
{
Singleton singleton = Singleton.getInstance();
Singleton singleton1 = Singleton.getInstance();

System.out.println(singleton == singleton1);
}
}

class Singleton
{
private static Singleton singleton = new Singleton();
private Singleton()
{

}

public static Singleton getInstance()
{
return singleton;
}

}
it means whenerver you new a Object,it returns the same
object address, i'ts Singleton Pattern. so your object is
immutable class

Is This Answer Correct ?    0 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why synchronization is important in java?

562


Difference between operator overloading and function overloading

612


Explain the importance of join() method in thread class?

639


Explain OOPs concept.

651


What do you mean by composition in java?

566






What is arrays aslist in java?

519


What is collection class in java? List down its methods and interfaces.

529


Explain the available thread states in a high-level?

528


What are multiple inheritances?

581


Why there is no call by reference in java?

494


what is the difference between yielding and sleeping? : Java thread

523


Whats the difference between notify() and notifyall()?

570


What is meant by bytecode?

574


What is flag in java?

531


How do you start a new line in java?

517