Java-Singleton Pattern

  • A Static Method is associated with Class(call class method) and can only directly invoke static methods and can only directly access static fields.
  • A normal method can access both static and instance methods and can access static and instance fields.
  • the modifier static should only used for main and test methods or specific situations
  • Static Field means there is always one exactly storage location for that field
    • storage in static area of memory(not in object )
    • accessed by A.a
    • used for constants

 

  • Singleton Pattern
    • create a single instance of a class that is public available
    • usually used as a storage(map, set, tree, LinkedList, ArrayList)
    • everything is private except storage constructor and class itself
      • if (storage == null){ storage a =new storage; return a; }

留下评论