This is a new concepts in java to control the full Application with a single object. That is from the Starting point of the application to the End point.
Our application should allow us to create object only once till the application Ends. How could that be done? First create a class(MyCalssName) with Static variable(myClassName). Declare the constructor of class as Private. Hence object for the class cannot be created outside of the class. Then how to create object for our class? At this point we use a Static method (getInstance) to create the instance of the Class. This method is Static because a Static method can be called or used with out an object and can be accessed by the class name. There is a If condition inside the method to check whether the variable is null or not. If null then the condition will return the existing object.
To create the instance of the class
MyClassName.getInstance();
To close the Object or to make the Object null like
MyClassName.getInstance().close();
To get the renew Object for the class
MyClassName.getInstance().renew();
Class MyClassName{
Private MyClassName{
}
Private static MyClassName myClassName = null;
Public static MyClassName getInstance(){
If(myClassName == null){
MyClassName = new MyClassName();
}
return myClassName;
}
public void renew(){
MyClassName.myclassName = new MyClassName();
}
public void close(){
MyClassName.myclassName = null;
}
}
Wednesday, August 26, 2009
Subscribe to:
Posts (Atom)