Wednesday, 6 December 2017

What is Singleton Class in Java?

o    A singleton class has only single instance
o    A single object can be used by all other classes.
o    It is used in multi-threaded and database applications, configuration settings etc.
o Singleton class should have static member , private constructor and static method

        public class SingleTon1{
         private static SingleTon1 obj;                    //Static member
       
           private SingleTon1(){                              //Private Constructor
                System.out.println("Hi User");
                      }

         public static SingleTon1 getInstance(){            //Static method
                        if(obj == null){
                        obj = new  SingleTon1();
                              }
                      return  obj;
                             }


       public void Display(){
       System.out.println("Welcome to LearingPoint92");
                                         }

    
       public static void main(String a[]){
       SingleTon1 st =SingleTon1.getInstance()
         st.Display();
                            }
                        }

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...