The constructor of a class is usually the same as the class name.
The constructor does not declare the return type.
Generally, constructors are always public. If the type is private, the class cannot be instantiated by an external class or object. It is often used in Sington mode. It is also commonly used for classes that only contain static members. In this case, a sealed modifier is usually added to the class.
Do not initialize the class instance in the constructor or explicitly call the constructor.
Generally, Singleton mode has several forms:
The first form:
Public class Singleton
{
Private Singleton (){}
// Define your own instance internally
// Note that this is private for internal calls only
Private static Singleton instance = new Singleton ();
// Here is a static method for external access to this class, which can be accessed directly.
Public static Singleton getInstance ()
{
Return instance;
}
}
Second form:
Public class Singleton
{
Private static Singleton instance = null;
Public static synchronized Singleton getInstance ()
{
// This method is better than above. You do not need to generate objects every time. It is only for the first time that an instance is generated, which improves the efficiency!
If (instance = null)
Instance = new Singleton ();
Return instance;
}
}
Referenced from:
Http://www2.cnblogs.com/netfork/archive/2004/03/22/3845.html