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

Is it possible for yielded thread to get chance for its execution again ?

555


What is lastindexof in java?

555


How do I know if java is installed?

523


What is the purpose of using the java bean?

578


What do you understand by casting in java language?

584






Why isn’t there operator overloading?

609


can I implement my own start() method? : Java thread

562


How does sublist works in java?

554


Explain java coding standards for variables ?

673


Does constructor be static?

572


a thread is runnable, how does that work? : Java thread

519


What are advantages of exception handling in java?

594


When will we prefer to use set and list in java and why?

550


Can we overload final method in java?

562


Difference between a process and a program?

621