-1
// java version: java17
public class SingleTon {
    
    private SingleTon(){}

    //What is the difference between a static inner class and an inner class in the singleton pattern?
    //private static class InnerClass{
    private class InnerClass{
        public static SingleTon instance = new SingleTon();
    }
    public static SingleTon getInstance(){
        return InnerClass.instance;
    }
}
public class Test {
    public static void main(String[] args) {
        SingleTon instance1 = SingleTon.getInstance();
        SingleTon instance2 = SingleTon.getInstance();
        System.out.println(instance1 == instance2); //  result: true;
    }
}

Either a static inner class or an inner class makes the same result;

( https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html )

  • I don't need the instance of the inner class. I only need to invoke the method clinit in Class loading phase.

Is the static inner class necessary in the singleton pattern?

shsf
  • 13
  • 4
  • Why do you have a `SingleTon` variable _and_ an inner class? – dan1st Feb 07 '23 at 07:48
  • Through the inner class loading, the singleton pattern is realized. @dan1st – shsf Feb 07 '23 at 07:55
  • I was referring to the variable in [Revision 2](https://stackoverflow.com/revisions/75370219/2) which is now deleted so it doesn't matter. – dan1st Feb 07 '23 at 07:58

2 Answers2

0

A non-static inner class would be semantically equivalent to:

private class SingleTon$InnerClass{//private with the exception that everything in SingleTon itself can access everything in InnerClass
    public static SingleTon instance = new SingleTon();

    private SingleTon this$SingleTon;//not entirely sure about the name
    private SingleTon$InnerClass(SingleTon this$SingleTon){
        this.this$SingleTon=this$SingleTon;
    }
}

While a static inner class would be equivalent to:

private class SingleTon$InnerClass{//private with the exception that everything in SingleTon itself can access everything in InnerClass
    public static SingleTon instance = new SingleTon();
}

In other words, making it non-static adds a reference to an instance of the outer class that you don't need. If you want to create instances of non-static inner classes you need an instance of the outer class for it.

While this doesn't matter if you just access a static field, it is unnecessary to make it non-static.

dan1st
  • 12,568
  • 8
  • 34
  • 67
0

I will add, that in Java it is recommended to create Singleton Classes using Enum.

public enum SingleTon{
   INSTANCE;
   public void doSomeAction() { ... }
}

As Joshua Bloch wrote in Effective Java:

This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.

You can read more here Implementing Singleton with an Enum (in Java) and Item 3: Enforce the Singleton Property with a Private Constructor or an enum Type