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

Explain about object oriented programming and its features?

587


What is fundamental datatype?

523


define polymorphism in java

626


What is the memory leak in java?

521


How we can generate random numbers in java?

599






What isan abstract class and when do you use it?

833


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

550


What is the use of default method in interface in java?

545


Is void a data type?

534


Java is pass by value or pass by reference? Explain

528


Explain yield() method in thread class ?

613


How do you trim a space in java?

496


What is string builder?

464


is it possible to instantiate the math class?

522


What does indexof return in java?

531