代码:
package com.test.singleton; public class Singleton { private Singleton() {} private static class SingletonHolder{ private static Singleton uniqueInstance = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.uniqueInstance; } }
javap -v -p Singleton.class:
Classfile /home/workspace/sshLog/bin/com/test/singleton/Singleton.class
Last modified 2015-10-29; size 601 bytes
MD5 checksum 3cc377dd67a04f5faf4471e472f446b3
Compiled from "Singleton.java"
public class com.test.singleton.Singleton
SourceFile: "Singleton.java"
InnerClasses:
private static #26= #17 of #1; //SingletonHolder=class com/test/singleton/Singleton$SingletonHolder of class com/test/singleton/Singleton
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Class #2 // com/test/singleton/Singleton
#2 = Utf8 com/test/singleton/Singleton
#3 = Class #4 // java/lang/Object
#4 = Utf8 java/lang/Object
#5 = Utf8 <init>
#6 = Utf8 ()V
#7 = Utf8 Code
#8 = Methodref #3.#9 // java/lang/Object."<init>":()V
#9 = NameAndType #5:#6 // "<init>":()V
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 Lcom/test/singleton/Singleton;
#14 = Utf8 getInstance
#15 = Utf8 ()Lcom/test/singleton/Singleton;
#16 = Methodref #17.#19 // com/test/singleton/Singleton$SingletonHolder.access$0:()Lcom/test/singleton/Singleton;
#17 = Class #18 // com/test/singleton/Singleton$SingletonHolder
#18 = Utf8 com/test/singleton/Singleton$SingletonHolder
#19 = NameAndType #20:#15 // access$0:()Lcom/test/singleton/Singleton;
#20 = Utf8 access$0
#21 = Utf8 (Lcom/test/singleton/Singleton;)V
#22 = Methodref #1.#9 // com/test/singleton/Singleton."<init>":()V
#23 = Utf8 SourceFile
#24 = Utf8 Singleton.java
#25 = Utf8 InnerClasses
#26 = Utf8 SingletonHolder
{
private com.test.singleton.Singleton();
flags: ACC_PRIVATE
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #8 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 5: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/test/singleton/Singleton;
public static com.test.singleton.Singleton getInstance();
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=1, locals=0, args_size=0
0: invokestatic #16 // Method com/test/singleton/Singleton$SingletonHolder.access$0:()Lcom/test/singleton/Singleton;
3: areturn
LineNumberTable:
line 14: 0
LocalVariableTable:
Start Length Slot Name Signature
com.test.singleton.Singleton(com.test.singleton.Singleton);
flags: ACC_SYNTHETIC
Code:
stack=1, locals=2, args_size=2
0: aload_0
1: invokespecial #22 // Method "<init>":()V
4: return
LineNumberTable:
line 5: 0
LocalVariableTable:
Start Length Slot Name Signature
}
javap -v -p Singleton\$SingletonHolder.class:
Classfile /home/workspace/sshLog/bin/com/test/singleton/Singleton$SingletonHolder.class
Last modified 2015-10-29; size 684 bytes
MD5 checksum db879168de181f375c9131d5220f6bd9
Compiled from "Singleton.java"
class com.test.singleton.Singleton$SingletonHolder
SourceFile: "Singleton.java"
InnerClasses:
private static #29= #1 of #10; //SingletonHolder=class com/test/singleton/Singleton$SingletonHolder of class com/test/singleton/Singleton
minor version: 0
major version: 51
flags: ACC_SUPER
Constant pool:
#1 = Class #2 // com/test/singleton/Singleton$SingletonHolder
#2 = Utf8 com/test/singleton/Singleton$SingletonHolder
#3 = Class #4 // java/lang/Object
#4 = Utf8 java/lang/Object
#5 = Utf8 uniqueInstance
#6 = Utf8 Lcom/test/singleton/Singleton;
#7 = Utf8 <clinit>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Class #11 // com/test/singleton/Singleton
#11 = Utf8 com/test/singleton/Singleton
#12 = Methodref #10.#13 // com/test/singleton/Singleton."<init>":(Lcom/test/singleton/Singleton;)V
#13 = NameAndType #14:#15 // "<init>":(Lcom/test/singleton/Singleton;)V
#14 = Utf8 <init>
#15 = Utf8 (Lcom/test/singleton/Singleton;)V
#16 = Fieldref #1.#17 // com/test/singleton/Singleton$SingletonHolder.uniqueInstance:Lcom/test/singleton/Singleton;
#17 = NameAndType #5:#6 // uniqueInstance:Lcom/test/singleton/Singleton;
#18 = Utf8 LineNumberTable
#19 = Utf8 LocalVariableTable
#20 = Methodref #3.#21 // java/lang/Object."<init>":()V
#21 = NameAndType #14:#8 // "<init>":()V
#22 = Utf8 this
#23 = Utf8 Lcom/test/singleton/Singleton$SingletonHolder;
#24 = Utf8 access$0
#25 = Utf8 ()Lcom/test/singleton/Singleton;
#26 = Utf8 SourceFile
#27 = Utf8 Singleton.java
#28 = Utf8 InnerClasses
#29 = Utf8 SingletonHolder
{
private static com.test.singleton.Singleton uniqueInstance;
flags: ACC_PRIVATE, ACC_STATIC
static {};
flags: ACC_STATIC
Code:
stack=3, locals=0, args_size=0
0: new #10 // class com/test/singleton/Singleton
3: dup
4: aconst_null
5: invokespecial #12 // Method com/test/singleton/Singleton."<init>":(Lcom/test/singleton/Singleton;)V
8: putstatic #16 // Field uniqueInstance:Lcom/test/singleton/Singleton;
11: return
LineNumberTable:
line 9: 0
LocalVariableTable:
Start Length Slot Name Signature
private com.test.singleton.Singleton$SingletonHolder();
flags: ACC_PRIVATE
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #20 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 7: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/test/singleton/Singleton$SingletonHolder;
static com.test.singleton.Singleton access$0();
flags: ACC_STATIC, ACC_SYNTHETIC
Code:
stack=1, locals=0, args_size=0
0: getstatic #16 // Field uniqueInstance:Lcom/test/singleton/Singleton;
3: areturn
LineNumberTable:
line 9: 0
LocalVariableTable:
Start Length Slot Name Signature
}
Singleton.getInstance调用:
0: invokestatic #16 // Method com/test/singleton/Singleton$SingletonHolder.access$0:()Lcom/test/singleton/Singleton;
这时第一次会加载私有内部类:Singleton$SingletonHolder,然后执行静态方法
static {};
flags: ACC_STATIC
Code:
stack=3, locals=0, args_size=0
0: new #10 // class com/test/singleton/Singleton
3: dup
4: aconst_null
5: invokespecial #12 // Method com/test/singleton/Singleton."<init>":(Lcom/test/singleton/Singleton;)V
8: putstatic #16 // Field uniqueInstance:Lcom/test/singleton/Singleton;
11: return
LineNumberTable:
line 9: 0
LocalVariableTable:
Start Length Slot Name Signature
初始化了uniqueInstance实例
然后access$0方法会返回静态初始化的uniqueInstance实例。这样和饿汉式木有什么区别了,但是也达到了LazyLoad的效果了。。。