What is singleton class?

Answer Posted / srinivas pentakota

A singleton is an class that can be instantiated once, and
only once. This is a fairly unique property, but useful in a
wide range of object designs. Creating an implementation of
the singleton pattern is fairly straightforward - simple
block off access to all constructors, provide a static
method for getting an instance of the singleton, and prevent
cloning.


public class SingletonObject
{
private SingletonObject()
{
// no code req'd
}

public static SingletonObject getSingletonObject()
{
if (ref == null)
// it's ok, we can call this constructor
ref = new SingletonObject();
return ref;
}

private static SingletonObject ref;
}

Is This Answer Correct ?    27 Yes 14 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is functional interface in java?

509


Is it possible to compare various strings with the help of == operator?

543


For ease of programming you can consider the maze as a 2D array with colors represented by below integer and characters (in capital letters). • B - Black • W -White • G- Green • R- Red R B W B W W W W W W B W B B W W W W W W W B W B W W W B W W W W B B W W W B W W W B W W B B B B W B W B W W B W W W B W W W B B B W W B W W W B W W B W B W W W B W B W W W W B B W W W W B W W W W W G Shortest Route Problem: • Solution that finds the shortest Route between Red and Green  White will have 1 Weight.  Red and Green carry no weights.  Shortest path is the path with less weight when you add up the weights in the path.

1579


What is the difference between preemptive scheduling and time slicing?

581


what is the difference between preemptive scheduling and time slicing? : Java thread

527






What are the advantages of user defined functions?

548


Why are getters and setters used?

563


Explain reverse a linked list recursive java solution?

521


What is the use of a copy constructor?

570


Can a constructor have different name than a class name in java?

590


What is the purpose of file class?

539


What is parameter example?

540


What is the purpose of javac exe?

555


How to sort an array in java without using sort method?

522


What is passed by reference and pass by value ?

577