Given the following simple code in Java.
final class Demo
{
public static final long serialVersionUID=1L;
static
{
System.out.println("Static constructor invoked.");
}
}
public final class Main
{
public static void main(String... args)
{
System.out.println(Demo.serialVersionUID);
}
}
In this simplest of Java code, it is expected that the constructor staticwill be called when the class is Demoinitialized by the method main()through Demo.serialVersionUID, but not .
If this program started without changes, the output would be only 1(the message caused by the Static constructor, as indicated in the block static, will not be displayed).
If we want the message to be printed as indicated in the initializer static, then we need to change the declaration statement in the class Demo,
public static final long serialVersionUID=1L;
or,
public static long serialVersionUID=1L;
modifier deletion finalor
public static final long serialVersionUID=1L;
changing the primitive type longto the corresponding shell type long.
, ? static ?