I recently became interested in Java bytecode and I created the following Hello World program.
The "Hello World" is printed, however I can't figure out why "Creating awesome object" isn't printed. Can you help me figure out what I'm doing wrong?
.class public HelloWorld
.super java/lang/Object
.method public <init>()V
.limit stack 2
aload_0 ; push 'this' unto stack
invokespecial java/lang/Object/<init>()V ; invoke constructor
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "Creating awesome object!"
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
return
.end method
.method public static main([Ljava/lang/String;)V
.limit stack 2
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "Hello world"
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
return
.end method
I don't know much about Java bytecode, but it seems to me you don't create an instance of HelloWorld
in main
. If you don't create an instance, the constructor does not get called, so there you have it. :)