I wrote a few study notes before, but reading these things is always very "mysterious". Let's reveal the veil of "JVM" in an actual practice, let the children's shoes in the "ssh" era get familiar with Java's "old ancestor" JVM. Due to your limited level, you may have any questions during the reading process, or if you have any questions, please submit them in time and I will solve them in time. If you have any suggestions, it is better for everyone to discuss them.
1. Source Code File
Public class learningclassfile {// common variable private int id1; // static variable Private Static int Id2; // constant private final int ID3 = 4; // static constant Private Static final int ID4 = 5; Public learningclassfile () {} public learningclassfile (INT id1, int Id2) {This. id1 = id1; this. id2 = Id2;} // use the public-modified addpub method public void addpub (int A, int B) {int result = a + B; system. out. println (result);} // use the private-modified addpri method private void addpri (int A, int B) {int result = a + B; system. out. println (result);} // public static void addsta () {int result = Id2 + ID4; system. out. println (result);} public static final void addfinal (int A, int B) {int result = a + B; system. out. println (result);} public static void main (string [] ARGs) {learningclassfile LCF = new learningclassfile (1, 2); LCF. addpub (1, 2); LCF. addpri (1, 2); addsta (); addfinal (1, 2 );}}
Class file:
Compiled from "learningclassfile. java "public class learningclassfile extends Java. lang. object sourcefile: "learningclassfile. java "minor version: 0 major version: 50 // runtime constant pool: used to store various literal and symbolic references generated during compilation. Constant pool: // default constructor inherited from the parent class Object // observe the features of this method: No parameter, return type voidconst #1 = method #13. #35; // Java/lang/object. "<init>" :() V // constant ID3 // "#7. #36; // learningclassfile. ID3: I "// #7: Find the class name learningclassfile in the constant pool // #36 -->" const #36 = nameandtype #17: #15; // ID3: I "// nameandtype literally indicates the name and type. That is, ID3 is the name of the variable. I indicates that ID3 is of the int type. // The overall description: ID3 in learningclassfile is of the int type. Const #2 = field #7. #36; // learningclassfile. ID3: iconst #3 = field #7. #37; // learningclassfile. id1: iconst #4 = field #7. #38; // learningclassfile. id2: I // store the out of the system to the constant pool // The out modified by public static final in the system class // "public final static printstream out = nullprintstream (); "// Description: The out attribute of the system class is printstream const #5 = field #39. #40; // Java/lang/SY Stem. out: ljava/IO/printstream; // store the println () method of printstream to the constant pool. // The parameter of this method is I, and the return value is voidconst #6 = method #41. #42; // Java/IO/printstream. println :( I) V // class learningclassfileconst #7 = Class #43; // learningclassfile // constructor // you need to input two int type variables const #8 = method #7. #44; // learningclassfile. "<init>" :( II) V // addpub method of learningclassfile // #4 --> "const #45 = nameandtype #27: #26; // addpub :( II) V "// #27 -->" const #27 = ASC Iz addpub; "method name: addpub // #26 -->" const #26 = asciz (ii) V; "method type: Two int type parameters, the return type is voidconst #9 = method #7. #45; // learningclassfile. addpub :( II) vconst #10 = method #7. #46; // learningclassfile. addpri :( II) vconst #11 = method #7. #47; // learningclassfile. addsta :() vconst #12 = method #7. #48; // learningclassfile. addfinal :( II) vconst #13 = Class #49; // Java/lang/objectconst #14 = asciz id1; const #15 = Asciz I; const #16 = asciz Id2; const #17 = asciz ID3; // The constantvalue attribute indicates the value of a constant field // that is, the final modified attribute const #18 = asciz constantvalue; // for final modified constants, the type and value are directly stored in the constant pool const #19 = int 4; const #20 = asciz ID4; const #21 = int 5; const #22 = asciz <init>; const #23 = asciz () V; // The Code attribute is only one method, the instance class initialization method, or the class initialization method. Save the Java VM commands and related auxiliary information. // In short: save the compiled Command Information const #24 = asciz code; // the corresponding table const #25 = asciz linenumber for the Java source code line number and the compiled bytecode command Table; const #26 = asciz (ii) V; const #27 = asciz addpub; const #28 = asciz addpri; const #29 = asciz addsta; const #30 = asciz addfinal; const #31 = asciz main; const #32 = asciz ([ljava/lang/string;) V; // Java source code file const #33 = asciz sourcefile; const #34 = asciz learningclassfile. java; const #35 = nameandtype #22: #23; // "<init>" :() vconst #36 = nameandtype #17: #15; // ID3: iconst #37 = nameandtype #14: #15; // I D1: iconst #38 = nameandtype #16: #15; // Id2: iconst #39 = Class #50; // Java/lang/systemconst #40 = nameandtype #51: #52; // out: ljava/IO/printstream; const #41 = Class #53; // Java/IO/printstreamconst #42 = nameandtype #54: #55; // println :( I) vconst #43 = asciz learningclassfile; const #44 = nameandtype #22: #26; // "<init>" :( II) vconst #45 = nameandtype #27: #26; // addpub :( II) vconst #46 = nameandtype #28: #2 6; // addpri :( II) vconst #47 = nameandtype #29: #23; // addsta :() vconst #48 = nameandtype #30: #26; // addfinal :( II) vconst #49 = asciz Java/lang/object; const #50 = asciz Java/lang/system; const #51 = asciz out; const #52 = asciz ljava/IO/printstream; const #53 = asciz Java/IO/printstream; const #54 = asciz println; const #55 = asciz (I) V; {// default constructor: Public learningclassfile (); Code: Stack = 2, locals = 1, args_siz E = 1 0: aload_0 1: invokespecial #1; // method Java/lang/object. "<init>" :() V // push ID3 reference to stack Top 4: aload_0 // Push 4 to stack top 5: iconst_4 // assign 4 to ID3 6: putfield #2; // field ID3: I 9: Return linenumbertable: Line 11: 0 // public learningclassfile () {// instance variables of the final type are initialized once in each constructor. Line 7: 4 // Private Final int ID3 = 4; line 12: 9 //} public learningclassfile (INT, INT); Code: Stack = 2, locals = 3, args_size = 3 0: aload_0 1: invokespecial #1; // method Java/lang/object. "<init>" :() V 4: aload_0 5: iconst_4 6: putfield #2; // field ID3: I 9: aload_0 10: iload_1 11: putfield #3; // field id1: I 14: aload_0 15: Pop 16: iload_2 17: putstatic #4; // field Id2: I 20: Return linenumbertable: Lin E 14: 0 // public learningclassfile (INT id1, int Id2) {// instance variables of the final type are initialized once in each constructor. Line 7: 4 // Private Final int ID3 = 4; line 15: 9 // This. id1 = id1; line 16: 14 // This. id2 = Id2; line 17: 20 //} public void addpub (INT, INT); Code: Stack = 2, locals = 4, args_size = 3 0: iload_1 1: iload_2 2: iadd 3: istore_3 4: getstatic #5; // field Java/lang/system. out: ljava/IO/printstream; 7: iload_3 8: invokevirtual #6; // method Java/IO/printstream. println :( I) V 11: Return linenumbertable: Line 21: 0 // int result = a + B; line 22: 4 // system. out. println (result); line 23: 11 //} public static void addsta (); Code: Stack = 2, locals = 1, args_size = 0 // get static variable Id2 push to top of stack 0: getstatic #4; // field Id2: I // pull the ID4 value 5 from the constant pool and push it to the top of the stack 3: iconst_5 // execute the add operation 4: iadd // push the calculation result to the top of the stack 5: istore_0 // obtain static and out 6: getstatic #5; // field Java/lang/system. out: ljava/IO/printstream; // obtain the calculation result 9: iload_0 // call the println Method 10: invok Evirtual #6; // method Java/IO/printstream. println :( I) V // method ends normally 13: Return linenumbertable: Line 33: 0 // int result = Id2 + ID4; line 34: 6 // system. out. println (result); line 35: 13 //} public static final void addfinal (INT, INT); Code: Stack = 2, locals = 3, args_size = 2 0: iload_0 1: iload_1 2: iadd 3: istore_2 4: getstatic #5; // field Java/lang/system. out: ljava/IO/printstream; 7: iload_2 8: invokpa Irtual #6; // method Java/IO/printstream. println :( I) V 11: Return linenumbertable: Line 38: 0 line 39: 4 Line 40: 11 public static void main (Java. lang. string []); Code: Stack = 4, locals = 2, args_size = 1 // create a learningclassfile object and push the object reference to the top of the stack 0: New #7; // class learningclassfile // back up the object reference and push it to the top of the stack // call the instance method with the original reference value, the reference value placed at the top of the stack will be overwritten by the following operations. 3: DUP // push parameter 1 in the constructor to stack Top 4: iconst_1 5: iconst_2 // execute constructor 6: invokespecial #8; // method "<init>" :( II) V // store the top reference value of the stack to the second local variable 9: astore_1 10: aload_1 11: iconst_1 12: iconst_2 // call the instance method 13: invokevirtual #9; // method addpub :( II) V 16: aload_1 17: iconst_1 18: iconst_2 19: invokespecial #10; // method addpri :( II) V // call Static Method 22: invokestatic #11; // method addsta :() V 25: iconst_1 26: iconst_2 27: invokestat IC #12; // method addfinal :( II) V 30: Return linenumbertable: Line 43: 0 // learningclassfile LCF = new learningclassfile (1, 2); line 44: 10 // LCF. addpub (1, 2); line 45: 16 // LCF. addpri (1, 2); line 46: 22 // addsta (); line 47: 25 // addfinal (1, 2); line 48: 30 //} differences between final variables and static final variables: 1. differences between instance constants and class constants 2. different methods of initial recognition: From the Perspective of class bytecode, final modified variables will be initialized in each constructor; static final variables must be initialized during definition. Understanding "the compilation phase shows that the runtime remains unchanged": the compiler can determine the version of the method to be called. There are two main methods that comply with this standard: Private method and static method. For details, see: deep understanding of JVM Reading Notes-bytecode execution engine.
2. differences between final variables and static final variables: (1) differences between instance constants and class constants (2) Different initialization methods: from the class bytecode, final modified variables will be initialized in each constructor. Static final variables must be initialized during definition.
3. Understand "the compilation period is known, and the runtime remains unchanged": the compiler can determine the version of the method to be called. There are two main methods that comply with this standard: Private method and static method. For details, see: deep understanding of JVM Reading Notes-bytecode execution engine.