I am trying to understand the structure of .class file in Java.
Wrote a very simple program :
package javautilities;
public class Class_File_Structure {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = a + b;
}
}
used the command javap -verbose Class_File_Structure.class
and the results are :
/*
Classfile /C:.../Class_File_Structure.class
Last modified 31 mar 2014; size 499 bytes
MD5 checksum 25f2237144af07e2832d78f4b16e015a
Compiled from "Class_File_Structure.java"
public class javautilities.Class_File_Structure
SourceFile: "Class_File_Structure.java"
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #3.#21 // java/lang/Object."<init>":()V
#2 = Class #22 // javautilities/Class_File_Structure
#3 = Class #23 // java/lang/Object
#4 = Utf8 <init>
#5 = Utf8 ()V
#6 = Utf8 Code
#7 = Utf8 LineNumberTable
#8 = Utf8 LocalVariableTable
#9 = Utf8 this
#10 = Utf8 Ljavautilities/Class_File_Structure;
#11 = Utf8 main
#12 = Utf8 ([Ljava/lang/String;)V
#13 = Utf8 args
#14 = Utf8 [Ljava/lang/String;
#15 = Utf8 a
#16 = Utf8 I
#17 = Utf8 b
#18 = Utf8 c
#19 = Utf8 SourceFile
#20 = Utf8 Class_File_Structure.java
#21 = NameAndType #4:#5 // "<init>":()V
#22 = Utf8 javautilities/Class_File_Structure
#23 = Utf8 java/lang/Object
{
public javautilities.Class_File_Structure();
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 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Ljavautilities/Class_File_Structure;
public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=4, args_size=1
0: bipush 10
2: istore_1
3: bipush 20
5: istore_2
6: iload_1
7: iload_2
8: iadd
9: istore_3
10: return
LineNumberTable:
line 6: 0
line 7: 3
line 8: 6
line 9: 10
LocalVariableTable:
Start Length Slot Name Signature
0 11 0 args [Ljava/lang/String;
3 8 1 a I
6 5 2 b I
10 1 3 c I
}
*/
I read that the format should be
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
but can someone explain the output because it is not very clear to me.