lundi 28 mars 2016

Singleton design pattern, static access non static?

I think to a common Singleton Design Pattern:

public class Singleton{

   private static Singleton instance;

   private Singleton(){}

   public static Singleton getInstance(){

    if(instance==null)
       instance=new Singleton();

   return instance;

   }
}

as far as I know, costructors are NON static methods because they can use the context reference "this" (which is forbidden in static contexts). On the other hand, static members can access only static members.

So how is it possible that static member getInstance() is accessing the non static member constructor?

Aucun commentaire:

Enregistrer un commentaire