Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

How to implement Singleton

Answer Posted / dara

Ensure a class has only one instance, and provide a global
point of access to it. Singletons maintain a static
reference to the sole singleton instance and return a
reference to that instance from a static instance() method.

Example:
========

public class MyClassSingleton {

private static MyClassSingleton instance;

//Constructor must be protected or private to perevent
creating new object
protected MyClassSingleton() {

}
//could be synchronized
public static MyClassSingletongetInstance() {
if (instance==null)
instance = new MyClassSingleton()
return instance;

}
public void method1(){
System.out.println("hello singleton");
}
}//end of class

Is This Answer Correct ?    1 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can you start a thread twice in Java?

1077


What is the purpose of interface?

967


What class allows you to read objects directly from a stream?

1297


Can we declare a class as static?

1054


What is the difference between an object-oriented programming language and object-based programming language?

962


What is nan in java?

955


What is a modifier?

1444


Can we have more than one package statement in the source file?

1029


Differences between C and Java?

1139


Are arrays static in java?

1023


What is the return type of the main method?

995


What are JVM.JRE, J2EE, JNI?

1065


Does java support function overloading, pointers, structures, unions or linked lists?

1059


Is empty list java?

885


What is the difference between a method and a procedure?

1015