add demo for how new keyword working

This commit is contained in:
Jason Lu 2025-02-17 13:36:44 +08:00
parent cc0ecc57bb
commit 103b75b80b

27
src/jvm/ObjectCreate.java Normal file
View File

@ -0,0 +1,27 @@
package jvm;
/**
* This class is intended to demo how jvm create object and invoke constructor. Please see byte code.
* use javap -v ObjectCreate.class or javap -c ObjectCreate.class to see byte code
*/
public class ObjectCreate {
private String field;
// will invoked by invokespecial
public ObjectCreate(String field) {
this.field = field;
}
public void print() {
show();
}
private void show() {
System.out.println(field);
}
public static void main(String[] args) {
ObjectCreate objectCreate = new ObjectCreate("hello!");
objectCreate.print();
}
}