|
| 1 | +# Java class file structure by example |
| 2 | +As you already know from "Class File Format" document, Java classes have a relatively complex structure. And the best way to study it - compile something and see these beatiful files in the wild! |
| 3 | + |
| 4 | +For such a purpose, you can use `javac` and `javad` command line tools - official compiler and disassembler, that are included into every Java Development Kit. |
| 5 | + |
| 6 | +However, there is a much more convinient method - [javap.yawk.at](https://javap.yawk.at) website! |
| 7 | + |
| 8 | +It lets you to both compile and disassemble your Java code in one click! |
| 9 | + |
| 10 | +For the beginning, let's compile a simple class. |
| 11 | + |
| 12 | +Something like **this**: |
| 13 | +```java |
| 14 | +class Main {} |
| 15 | +``` |
| 16 | +<details> |
| 17 | + <summary>Disassembler output</summary> |
| 18 | + |
| 19 | + |
| 20 | + Classfile /tmp/1059625032030680147/classes/Main.class |
| 21 | + Last modified Aug 7, 2020; size 237 bytes |
| 22 | + SHA-256 checksum <> |
| 23 | + Compiled from "Main.java" |
| 24 | + class Main |
| 25 | + minor version: 0 |
| 26 | + major version: 58 |
| 27 | + flags: (0x0020) ACC_SUPER |
| 28 | + this_class: #7 // Main |
| 29 | + super_class: #2 // java/lang/Object |
| 30 | + interfaces: 0, fields: 0, methods: 1, attributes: 1 |
| 31 | + |
| 32 | + Constant pool: |
| 33 | + #1 = Methodref #2.#3 // java/lang/Object."<init>":()V |
| 34 | + #2 = Class #4 // java/lang/Object |
| 35 | + #3 = NameAndType #5:#6 // "<init>":()V |
| 36 | + #4 = Utf8 java/lang/Object |
| 37 | + #5 = Utf8 <init> |
| 38 | + #6 = Utf8 ()V |
| 39 | + #7 = Class #8 // Main |
| 40 | + #8 = Utf8 Main |
| 41 | + #9 = Utf8 Code |
| 42 | + #10 = Utf8 LineNumberTable |
| 43 | + #11 = Utf8 LocalVariableTable |
| 44 | + #12 = Utf8 this |
| 45 | + #13 = Utf8 LMain; |
| 46 | + #14 = Utf8 SourceFile |
| 47 | + #15 = Utf8 Main.java |
| 48 | + |
| 49 | + { |
| 50 | + Main(); |
| 51 | + descriptor: ()V |
| 52 | + flags: (0x0000) |
| 53 | + |
| 54 | + Code: |
| 55 | + |
| 56 | + stack=1, locals=1, args_size=1 |
| 57 | + start local 0 s// Main this |
| 58 | + 0: aload_0 |
| 59 | + 1: invokespecial #1 // Method java/lang/Object."<init>":()V |
| 60 | + 4: return |
| 61 | + end local 0 // Main this |
| 62 | + |
| 63 | + LineNumberTable: |
| 64 | + |
| 65 | + LocalVariableTable: |
| 66 | + |
| 67 | + Start Length Slot Name Signature |
| 68 | + 0 5 0 this LMain; |
| 69 | + |
| 70 | + } |
| 71 | + SourceFile: "Main.java" |
| 72 | + |
| 73 | +</details> |
0 commit comments