There are two types of binders in Java, one is dynamic and one is static.
Dynamic linking: Also known as polymorphic or late-linking, because which function to invoke is not determined at compile time and is deferred to run. That is, the type of object pointed to by the pointer is determined when the program is run.
Static linking: Static linking refers to the work of the binder in the compile connection phase, which is also known as the early link, because this kind of work is done before the program starts running.
Conditions for static Union: Class method (static method), class variable (static type variable).
EXM:
public class par_test{//base class
public static int i=4;
Public Par_test () {//constructor
System.out.println ("Par_test initialzed");
}
public void Write () {//Dynamic Union
System.out.println ("Par_test write" +i);
}
public static void Writestatic () {//static method test static Union
System.out.println ("Par_test writestatic" +i);
}
}
public class Chi_test extends par_test{//subclass
public static int i=5;
Public Chi_test () {//constructor
System.out.println ("chi_test initialized");
}
public void Write () {//Dynamic Union
System.out.println ("Chi_test write" +i);
}
public static void Writestatic () {//static method test static Union
System.out.println ("Chi_test writestatic" +i);
}
}
public class test{
public static void Main (string[] pars) {
Pra_test Test = new Chi_test (); Test initialization Constructor Order
Test.write (); Test dynamic linking
Test.writestatic (); Test Static Union
}
}
Program execution Results:
Par_test initialized;
Chi_test initialized;
Chi_test Write 5;
Par_test writestatic 4;
The results of the program run can be seen:
1. When constructing a subclass object, the constructor of its parent class is called before the class constructor is executed.
2, for the general method even if the object is coerced into the parent class object, the program runs the method in the subclass.
3, for a static method, because at compile time determines its order of execution, so the program run time to execute the static method of the object after casting.
Java dynamic linking