[Java] About Singleton Class

Hello ^^ I'm Nguyen working for an IT company in Tokyo. It's my first post, so it's a little exciting. ^^ Here are some things to remember when studying Java SE8 Gold. Immediately, I would like to make a note about the singleton class.

What is a singleton

** Singleton ** is one of the design patterns that guarantees that no more than one instance of a class is created.

Therefore, singletons are generally implemented according to the guidelines listed below.

  1. Declare a private static field, create a single instance of itself and set it in that field. ⇒ Motivation: Create only one instance and always refer to only that one instance.
  2. Declare the constructor private. ⇒ Motivation: Prevent instantiation from outside (can be instantiated only inside the class)
  3. Provide a public static method that returns an instance set in its own field. ⇒ Motivation: To be able to get an instance reference of this class from the outside.

Singleton class implementation example:

Foo.java


public class Foo {
    private static final Foo foo = new Foo();
    private Foo();
    public static Foo getInstance() {
        return foo;
    }
}

Get an instance of a singleton class from the outside.

Foo foo = Foo.getInstance();

Finally

How about everyone? I hope you understand the singleton class even a little. ^^ See you in the next post! Bye bye!

Recommended Posts

[Java] About Singleton Class
About Java class
About class division (Java)
About Java String class
About java abstract class
About Java class loader types
About Java class variables class methods
Java class methods
[Java] About Java 12 features
[Java] Class inheritance
java Scanner class
[Java] About arrays
About class inheritance.
java (abstract class)
[Java] Singleton pattern
Something about java
Where about java
About Java features
[Java] Nested class
Java anonymous class
About Java threads
[Java] About interface
About Java arrays
About java inheritance
About interface, java interface
[java] abstract class
[Java] Object class
Java local class
About List [Java]
About java var
About Java literals
About Java commands
About Java log output
About Java functional interface
GetInstance () from a @Singleton class in Groovy from Java
About the StringBuilder class
About [Java] [StreamAPI] allMatch ()
About Java method binding
[Java] About anonymous classes
About method splitting (Java)
Java inner class review
[Java Silver] About initialization
About Java Array List
About Java Polymorphism super ()
[Swing] About JFrame class
Java class type field
About inheritance (Java Silver)
Java programming (class method)
About Java access modifiers
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
About sorting java.util.Arrays class
[Java] About enum type
Java programming (class structure)
All about Java programming
Find out about class methods
A note about Java GC
What I researched about Java 8
Java memo (standard class) substring