In depth analysis of Java static binding and dynamic binding

  • 2020-05-17 05:28:36
  • OfStack

Java static binding and dynamic binding

Recently, I have learned the knowledge of java, and my learning of the static binding and dynamic binding of java is very vague. Then I summarized the relevant knowledge of java under baidu 1 to help me master this part of knowledge

The concept of program binding:

Binding refers to the association of a method call with the class (method body) in which the method resides. For java, there are static binding and dynamic binding. Or early binding and late binding.

Static binding:

The method is bound before the program is executed (that is, it is known at compile time which class the method is in) and is implemented by the compiler or other linker. For example: C.

Simple for java can be understood as a binding at compile time; Note 1 that the only methods in java are final, static, private and the constructor are early-bound

Dynamic binding:

Late binding: binding at run time based on the type of the specific object.

If a language implements late binding, you must also provide mechanisms to determine the type of object at run time and to invoke the appropriate methods separately. That is, the compiler still does not know the type of the object, but the method invocation mechanism can investigate itself and find the correct method body. Different languages have different ways of implementing late binding. But let's at least think of it this way: they all want to insert some special type of information into the object.

Dynamic binding process:

The virtual machine extracts the method table of the actual type of the object; Virtual machine search method signature; Call the method.

The understanding of final, static, private and constructors is early binding

For the method of private, the first point is that it cannot be inherited. Since it cannot be inherited, it cannot be called through the object of its subclass, but only through the object of the class itself. So you can say that the private method is bound to the class that defines the method.

final method may be inherited, but it can't be rewritten (cover), although a subclass object can be called, but the call is defined in the parent class that final method, (from which we can know the method statement for final type, 1 is to prevent methods are covered, 2 is to effectively shut down the java dynamic binding).
Constructor is also cannot be inherited (online has said a subclass unconditionally inherit the no-arg constructor of the parent as a constructor, but personally think that this argument is not very appropriate, because we know that a subclass is through super () is used to call the superclass constructor, to complete the initialization of the parent class, and we use the parent class inherited method is to don't have to do this, so should not be said a subclass inherits the parent class constructor), so the compile time also can know that the constructor is belong to which class.

For the static method, I don't know exactly how it works. However, according to the information on the Internet and my own experiments, we can conclude that the static method can be inherited by subclasses, but it cannot be overridden by subclasses, but it can be hidden by subclasses. (this means that if there is an static method in the parent class and there is no corresponding method in its subclass, the method in the parent class will be used when the subclass object calls the method. If the same method is defined in a subclass, the method defined in the subclass is called. The only difference with 1 is that when a subclass object is transformed into a parent object, the static method in the parent class will be used regardless of whether or not the static method is defined in the subclass. So it says that static methods can be hidden and not overridden. This is similar to subclasses hiding member variables in their parent classes. The difference between hiding and overwriting is that when a subclass object is converted to a superclass object, it can access the variables and methods that the superclass is hiding, but not the methods that the superclass is overwriting.)

From the above we can conclude that if a method is not inheritable or cannot be overridden after inheritance, then the method is static bound.

Compile and run java

The compilation process of java is the process of compiling the java source files into bytecode (jvm executable code, i.e..class file). In this process, java does not deal with memory. In this process, the compiler will analyze the syntax, and if the syntax is not correct, it will report an error.

The Java run is when the jvm (java virtual machine) loads the bytecode file and interprets the execution. In this process is the actual creation of the memory layout, the execution of the java program.
java bytecode can be executed in two ways: (1) just-in-time compilation: the interpreter first compiles the bytes into machine code and then executes the machine code; (2) interpretation execution: the interpreter completes all operations of the java bytecode program by interpreting and executing 1 small piece of code at a time. (here we can see that the java program is actually converted twice during execution, first to bytecode and then to machine code. That's why java compiles once and runs everywhere. By installing the corresponding java virtual machine on different platforms, the same bytecode can be converted into machine code on different platforms, so as to run on different platforms.

As mentioned above, for the methods in java, except that final, static, private and constructors are early-stage bindings, the other methods are all dynamic bindings.
The typical dynamic binding occurs under the parent and subclass conversion declarations:

For example: Parent p = new Children();

The specific process details are as follows:

1: the compiler checks the declared type and method name of the object.

If we call the x.f (args) method, and x has been declared as an object of the C class, the compiler lists all the C methods named f and the f methods inherited from the C superclass.

2: next the compiler checks the parameter types provided in the method call.

If one of the parameter types in the method named f most closely matches the parameter type provided by the call, the method is called, in a process called "overload resolution."

3: when a program runs and calls a method using dynamic binding, the virtual machine must call the version of the method that matches the actual type of the object pointed to by x.

Assuming the actual type is D(a subclass of C), the method is called if the D class defines f(String), otherwise the method f(String) is searched in D's superclass, and so on.
When the JAVA virtual machine calls a class method (a static method), it selects the method to call based on the type of the object reference (usually known at compile time). Instead, when a virtual machine calls an instance method, it selects the method it calls based on the actual type of the object (which can only be known at runtime). This is known as dynamic binding, which is a form of polymorphism. Dynamic binding is a very elegant mechanism that provides great flexibility to solve real business problems.

Unlike the method, member variables (instance variables and class variables) in the java class are handled not with runtime binding, but with static binding in the general sense. So in the case of upward transformation, the object's methods can find the subclass, while the object's properties (member variables) are still the properties of the parent class (the subclass hides the parent class's member variables).


public class Father { 
  protected String name = " Father's properties "; 
} 
   
 
public class Son extends Father { 
  protected String name = " Son attributes "; 
 
  public static void main(String[] args) { 
    Father sample = new Son(); 
    System.out.println(" Properties of the call: " + sample.name); 
  } 
} 

Conclusion, the member of the call is the parent property.

This result shows that the object of the subclass (handle by reference to the parent class) is calling the member variable of the parent class. So it must be clear that the category for which the runtime (dynamic) binding is directed is only the method of the object.

Now what if you try to call the member variable name of the subclass? The easiest way to do this is to encapsulate the member variable into the method getter form.
The code is as follows:



public class Father { 
  protected String name = " Father's properties "; 
 
  public String getName() { 
    return name; 
  } 
}   
 
public class Son extends Father { 
  protected String name = " Son attributes "; 
 
  public String getName() { 
    return name; 
  } 
 
  public static void main(String[] args) { 
    Father sample = new Son(); 
    System.out.println(" Properties of the call :" + sample.getName()); 
  } 
} 

Result: the property of the son is called

java because of the static binding method to the property. This is because static binding has many benefits, allowing us to find bugs at compile time, rather than at run time. This can improve the running efficiency of the program! The dynamic binding of the method is to achieve polymorphism, which is a major feature of java. Polymorphism is also one of the key object-oriented techniques, so it is well worth the cost of efficiency to achieve polymorphism in java.

Note: most of the above content is from the Internet, a small part is personal opinion, not authoritative speech. If there is any improper expression or incorrect expression, please kindly advise.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: