Differences between static final, static, and final
Static: static, global, and class objects share a static variable, only initialized once.
Final: constant. If it is a method, it cannot be overwritten. If it is a class that cannot be inherited, it must be set during initialization. After setting, it cannot be changed.
Static final: static variable
The initialization sequence is as follows:
Parent static variables (static initialization blocks)> subclass static variables (static initialization blocks)> parent common variables (Common initialization blocks)> parent class constructor> parent class common methods
> Subclass common variable (Common initialization block)> subclass constructor> subclass common method
The following is an example to illustrate all this.
Package com. loulijun. test1;
Public class test1 {
Public static void main (string ARGs [])
{
Son son = new son ();
Son. Say ();
}
}
Class father
{
// Static final and static are not executed sequentially. They are executed from top to bottom in the code order.
// Static variables
Public static string STR = initstr ("FATHER: static variable --- 1 ");
// Static constant
Public static final int I = initfinal (2 );
// Common variable
Public Int J = Init (4 );
Static int initfinal (int I)
{
System. Out. println ("FATHER: static constant ---" + I );
Return I;
}
Static int Init (Int J)
{
System. Out. println ("FATHER: Common variable ---" + J );
Return J;
}
// Static initialization Block
Static
{
System. Out. println ("FATHER: static initialization block --- 3 ");
}
Static string initstr (string Str)
{
System. Out. println (STR );
Return STR;
}
// Initialize the block
{
System. Out. println ("FATHER: normal initialization block --- 5 ");
}
// Constructor
Public father ()
{
System. Out. println ("FATHER: parent class constructor --- 6 ");
}
Public void say ()
{
System. Out. println ("FATHER: normal method --- 7 ");
}
}
Class son extends father
{
// Static final and static are not executed sequentially. They are executed from top to bottom in the code order.
// Static variables
Public static string STR = initstr ("Son: static variable --- 1 ");
// Static constant
Public static final int I = initfinal (2 );
// Common variable
Public Int J = Init (4 );
Static int initfinal (int I)
{
System. Out. println ("Son: static constant ---" + I );
Return I;
}
Static int Init (int I)
{
System. Out. println ("Son: normal variable ---" + I );
Return I;
}
// Static initialization Block
Static
{
System. Out. println ("Son: static initialization block --- 3 ");
}
Static string initstr (string Str)
{
System. Out. println (STR );
Return STR;
}
// Initialize the block
{
System. Out. println ("Son: normal initialization block --- 5 ");
}
// Constructor
Public son ()
{
System. Out. println ("Son: subclass constructor 6 ");
}
Public void say ()
{
System. Out. println ("Son: normal method --- 7 ");
}
}
Running result:
Father: static variable --- 1
Father: static constant --- 2
Father: static initialization block --- 3
Son: static variable --- 1
Son: static constant --- 2
Son: static initialization block --- 3
Father: Common variable --- 4
Father: Common initialization block --- 5
Father: parent class constructor --- 6
Son: Common variable --- 4
Son: normal initialization block --- 5
Son: subclass constructor 6
Son: common method --- 7