Home > Archives > JVM New (创建对象)

JVM New (创建对象)

Publish:

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
public class Student {
    //成员变量
    String name = "Zhangsan";
    int age = 10;

    //构造方法
    public Student(){

    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    //成员代码块
    {
        name = "Lisi";
        age = 20;
    }

    public static void main(String[] args) {
        Student stu = new Student("Wangwu", 30);
        System.out.println(stu.name);
        System.out.println(stu.age);
    }
}
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home\bin\javap -v Student
Classfile /Users/baoguo/codes/demo/target/classes/Student.class
  Last modified Mar 27, 2022; size 949 bytes
  MD5 checksum d681bb32937e73f29729eb34556df6ef
  Compiled from "Student.java"
public class Student
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #12.#33        // java/lang/Object."<init>":()V
   #2 = String             #34            // Zhangsan
   #3 = Fieldref           #6.#35         // Student.name:Ljava/lang/String;
   #4 = Fieldref           #6.#36         // Student.age:I
   #5 = String             #37            // Lisi
   #6 = Class              #38            // Student
   #7 = String             #39            // Wangwu
   #8 = Methodref          #6.#40         // Student."<init>":(Ljava/lang/String;I)V
   #9 = Fieldref           #41.#42        // java/lang/System.out:Ljava/io/PrintStream;
  #10 = Methodref          #43.#44        // java/io/PrintStream.println:(Ljava/lang/String;)V
  #11 = Methodref          #43.#45        // java/io/PrintStream.println:(I)V
  #12 = Class              #46            // java/lang/Object
  #13 = Utf8               name
  #14 = Utf8               Ljava/lang/String;
  #15 = Utf8               age
  #16 = Utf8               I
  #17 = Utf8               <init>
  #18 = Utf8               ()V
  #19 = Utf8               Code
  #20 = Utf8               LineNumberTable
  #21 = Utf8               LocalVariableTable
  #22 = Utf8               this
  #23 = Utf8               LStudent;
  #24 = Utf8               (Ljava/lang/String;I)V
  #25 = Utf8               MethodParameters
  #26 = Utf8               main
  #27 = Utf8               ([Ljava/lang/String;)V
  #28 = Utf8               args
  #29 = Utf8               [Ljava/lang/String;
  #30 = Utf8               stu
  #31 = Utf8               SourceFile
  #32 = Utf8               Student.java
  #33 = NameAndType        #17:#18        // "<init>":()V
  #34 = Utf8               Zhangsan
  #35 = NameAndType        #13:#14        // name:Ljava/lang/String;
  #36 = NameAndType        #15:#16        // age:I
  #37 = Utf8               Lisi
  #38 = Utf8               Student
  #39 = Utf8               Wangwu
  #40 = NameAndType        #17:#24        // "<init>":(Ljava/lang/String;I)V
  #41 = Class              #47            // java/lang/System
  #42 = NameAndType        #48:#49        // out:Ljava/io/PrintStream;
  #43 = Class              #50            // java/io/PrintStream
  #44 = NameAndType        #51:#52        // println:(Ljava/lang/String;)V
  #45 = NameAndType        #51:#53        // println:(I)V
  #46 = Utf8               java/lang/Object
  #47 = Utf8               java/lang/System
  #48 = Utf8               out
  #49 = Utf8               Ljava/io/PrintStream;
  #50 = Utf8               java/io/PrintStream
  #51 = Utf8               println
  #52 = Utf8               (Ljava/lang/String;)V
  #53 = Utf8               (I)V
{
  java.lang.String name;
    descriptor: Ljava/lang/String;
    flags:

  int age;
    descriptor: I
    flags:

  public Student();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: aload_0
         5: ldc           #2                  // String Zhangsan
         7: putfield      #3                  // Field name:Ljava/lang/String;
        10: aload_0
        11: bipush        10
        13: putfield      #4                  // Field age:I
        16: aload_0
        17: ldc           #5                  // String Lisi
        19: putfield      #3                  // Field name:Ljava/lang/String;
        22: aload_0
        23: bipush        20
        25: putfield      #4                  // Field age:I
        28: return
      LineNumberTable:
        line 5: 0
        line 2: 4
        line 3: 10
        line 15: 16
        line 16: 22
        line 7: 28
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      29     0  this   LStudent;

  public Student(java.lang.String, int);
    descriptor: (Ljava/lang/String;I)V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=3, args_size=3
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: aload_0
         5: ldc           #2                  // String Zhangsan
         7: putfield      #3                  // Field name:Ljava/lang/String;
        10: aload_0
        11: bipush        10
        13: putfield      #4                  // Field age:I
        16: aload_0
        17: ldc           #5                  // String Lisi
        19: putfield      #3                  // Field name:Ljava/lang/String;
        22: aload_0
        23: bipush        20
        25: putfield      #4                  // Field age:I
        28: aload_0
        29: aload_1
        30: putfield      #3                  // Field name:Ljava/lang/String;
        33: aload_0
        34: iload_2
        35: putfield      #4                  // Field age:I
        38: return
      LineNumberTable:
        line 9: 0
        line 2: 4
        line 3: 10
        line 15: 16
        line 16: 22
        line 10: 28
        line 11: 33
        line 12: 38
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      39     0  this   LStudent;
            0      39     1  name   Ljava/lang/String;
            0      39     2   age   I
    MethodParameters:
      Name                           Flags
      name
      age

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=4, locals=2, args_size=1
         0: new           #6                  // class Student
         3: dup
         4: ldc           #7                  // String Wangwu
         6: bipush        30
         8: invokespecial #8                  // Method "<init>":(Ljava/lang/String;I)V
        11: astore_1
        12: getstatic     #9                  // Field java/lang/System.out:Ljava/io/PrintStream;
        15: aload_1
        16: getfield      #3                  // Field name:Ljava/lang/String;
        19: invokevirtual #10                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        22: getstatic     #9                  // Field java/lang/System.out:Ljava/io/PrintStream;
        25: aload_1
        26: getfield      #4                  // Field age:I
        29: invokevirtual #11                 // Method java/io/PrintStream.println:(I)V
        32: return
      LineNumberTable:
        line 20: 0
        line 21: 12
        line 22: 22
        line 23: 32
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      33     0  args   [Ljava/lang/String;
           12      21     1   stu   LStudent;
    MethodParameters:
      Name                           Flags
      args
}
SourceFile: "Student.java"

Process finished with exit code 0

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