Home > Archives > JVM static (静态代码块)

JVM static (静态代码块)

Publish:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Test {

    static int i = 100;

    static {
        i = 200;
    }

    static {
        i = 300;
    }

    public static void main(String[] args) {

    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home\bin\javap -v Test
Classfile /Users/baoguo/codes/demo/target/classes/Test.class
  Last modified Mar 26, 2022; size 490 bytes
  MD5 checksum 551366477529db6d914d84bbb8d902bc
  Compiled from "Test.java"
public class Test
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #4.#22         // java/lang/Object."<init>":()V
   #2 = Fieldref           #3.#23         // Test.i:I
   #3 = Class              #24            // Test
   #4 = Class              #25            // java/lang/Object
   #5 = Utf8               i
   #6 = Utf8               I
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Utf8               LineNumberTable
  #11 = Utf8               LocalVariableTable
  #12 = Utf8               this
  #13 = Utf8               LTest;
  #14 = Utf8               main
  #15 = Utf8               ([Ljava/lang/String;)V
  #16 = Utf8               args
  #17 = Utf8               [Ljava/lang/String;
  #18 = Utf8               MethodParameters
  #19 = Utf8               <clinit>
  #20 = Utf8               SourceFile
  #21 = Utf8               Test.java
  #22 = NameAndType        #7:#8          // "<init>":()V
  #23 = NameAndType        #5:#6          // i:I
  #24 = Utf8               Test
  #25 = Utf8               java/lang/Object
{
  static int i;
    descriptor: I
    flags: ACC_STATIC

  public Test();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   LTest;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=0, locals=1, args_size=1
         0: return
      LineNumberTable:
        line 15: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       1     0  args   [Ljava/lang/String;
    MethodParameters:
      Name                           Flags
      args

  static {};
    descriptor: ()V
    flags: ACC_STATIC
    Code:
      stack=1, locals=0, args_size=0
         0: bipush        100
         2: putstatic     #2                  // Field i:I
         5: sipush        200
         8: putstatic     #2                  // Field i:I
        11: sipush        300
        14: putstatic     #2                  // Field i:I
        17: return
      LineNumberTable:
        line 3: 0
        line 6: 5
        line 10: 11
        line 11: 17
}
SourceFile: "Test.java"

Process finished with exit code 0

声明: 本文采用 BY-NC-SA 授权。转载请注明转自: Ding Bao Guo