Initialization of Java virtual machine class loading
We know that “Java Virtual Machine Specification” chapter 5.5 Initialization ^(1)^ strictly stipulates that there are only six situations where the class must be initialized immediately:
Encountered new (instantiated object), getstatic (read a class static field which is not modified by final or does not put the result into the constant pool at compile time), putstatic (set a class static field which is not modified by final or does not put the result into the constant pool at compile time) or ...
The actual process of class initialization
Class initialization orderMany people know that the order of class initialization is as follows:(1) Call the base class constructor and repeat this process until the bottom layer(2) Then call the initialization methods of the members in the order of declaration(3) Call the body of the class constructor
The actual process of class initializationHowever, consider this example:
12345678910111213141516171819202122232425262728293031class Animal{ public void walk(){ System.out.println(" ...
Why anonymous inner class parameters must be final
Recently, when I wrote some anonymous inner classes, I felt a little difficult to understand, so I reviewed this part again. I found some points that I didn’t notice before - the formal parameters of the anonymous inner class must be prefixed with final (unless the anonymous inner class does not use it)
So, how to understand the principle behind this matter?
How inner classes workFirst, think about how inner classes work. We know that after the internal class is successfully compiled, it will ge ...