Deep analysis of Java static binding and dynamic binding _java

Source: Internet
Author: User
Tags instance method

Java static binding and dynamic binding

Recent Learning Java knowledge, static binding and dynamic binding of Java learning is very vague, and then on Baidu to its corresponding knowledge of the summary and collation, to help grasp the knowledge of this part

The concept of program binding:

Binding refers to the invocation of a method associated with the class (method body) in which the method is located. For Java, bindings are divided into static and dynamic bindings, or early binding and late binding.

Static binding:

The method is already bound before the program executes (that is, the method in which the method is actually known during compilation) is implemented by the compiler or other linker. For example: C.

For Java simple can be understood as the binding of the program compile-time; here is a point, Java in the method only Final,static,private and construction method is early bound

Dynamic binding:

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

If a language implements late binding, it must also provide mechanisms to determine the type of the object during run time, and to invoke the appropriate method, respectively. In other words, the compiler still does not know the type of object at this time, but the method invocation mechanism can investigate itself to find the correct method body. Different languages differ in how they are implemented in late binding. But we can at least think of it as: they all have special types of information to be placed in the object.

The process of dynamically binding:

    1. The virtual machine extracts the object's actual type of method table;
    2. Virtual machine Search method signature;
    3. Call method.

On the understanding of final,static,private and construction methods as early binding

For private methods, first of all, it cannot be inherited, and since it cannot be inherited, it cannot be invoked through the object of its subclass, but only through the object of the class itself. So it can be said that the private method is bound together with the class that defines the method.

The final method, although it can be inherited, cannot be overridden (overwritten), although the subclass object can be invoked, but the call is the final method defined in the parent class (from which we can know to declare the method as final type, and to prevent the method from being overwritten. The second is to effectively turn off dynamic binding in Java.
The construction method is also not inheritable (there is also an online subclass of the parameterless constructor that inherits the parent class unconditionally as its own constructor, but personally, it's not appropriate because we know that subclasses use super () to invoke the parameterless construction method of the parent class to initialize the parent class. Instead of using a method that inherits from the parent class, we should not say that the subclass inherits the constructor of the parent class, so it is also possible to know at compile time which class the constructor belongs to.

For the static method, the specific principle I also can not say too clearly. However, according to the online data and my own experiments can be concluded that the static method can be inherited by the quilt class, but can not be overridden (overridden), but may be hidden by the quilt class. (This means that if there is a static method in the parent class, if there is no corresponding method in its subclass, then the method in the parent class is used when the subclass object invokes the method.) If the same method is defined in a subclass, the method defined in the subclass is invoked. The only difference is that when a subclass object is transformed into a parent object, the object uses the static method in the parent class, regardless of whether or not the static method is defined in the child class. So this means that static methods can be hidden and not overwritten. This is the same as the subclass hides the member variable in the parent class. The difference between shadowing and overwriting is that when a subclass object is converted to a parent class object, it is able to access variables and methods that are hidden by the parent class and cannot access the method overridden by the parent class.

From the above we can conclude that if a method cannot be inherited or inherited and cannot be overwritten, then this method takes a static binding.

Compilation and operation of Java

Java's compilation process is the process of compiling Java source files into bytecode (JVM executable code, i.e.. class file), in which Java does not deal with memory, in which the compiler will parse the syntax, if the syntax is incorrect will be an error.

Java's running process refers to the JVM (Java Virtual machine) loading bytecode files and interpreting execution. In this process is really the creation of the memory layout, the implementation of Java programs.
There are two ways to execute Java bytecode: (1) just-in-time compilation: The interpreter first compiles the bytes into machine code, then executes the machine code, and (2) interprets the execution mode: The interpreter completes all operations of the Java bytecode program by interpreting and executing a small piece of code each time. (Here we can see that the Java program in the implementation of the process is actually a two conversion, first into a byte code and then converted to machine code.) This is why Java can compile and run everywhere. With the corresponding Java virtual machines on different platforms, the same bytecode can be converted to machine code on different platforms to run on different platforms.

As has been said in Java, in addition to the Final,static,private and construction method is early binding, all the other methods are dynamic binding.
The typical dynamic binding occurs under the transformation declaration of the parent class and subclass:

For example: Parent p = new Children ();

Details of its specific process are as follows:

1: The compiler checks the object's declaration type and method name.

Assuming we call the X.F (args) method, and X has been declared as an object of Class C, the compiler enumerates all methods in the C class with the name F and the F method inherited from the superclass Class C.

2: The compiler then checks the parameter types provided in the method invocation.

This method is called if there is a parameter type in all methods with the name F that best matches the type provided by the invocation, and this procedure is referred to as "overload resolution."

3: When the program is run and the method is invoked using dynamic binding, the virtual machine must invoke a method version that matches the actual type of the object that X points to.

Suppose the actual type is a subclass of D (c), and if the D class defines an F (string), the method is called, or the method F (string) is searched in the superclass of D, and so on.
When a JAVA virtual machine invokes a class method (static method), it chooses the method that is invoked based on the type of object reference (usually at compile time). Instead, when a virtual machine calls an instance method, it chooses the method that is invoked based on the actual type of the object (which can only be known at run time), which is dynamic binding, which is polymorphic. Dynamic binding provides a great deal of flexibility to solve real business problems and is a very graceful mechanism.

Unlike methods, when dealing with member variables (instance variables and class variables) in Java classes, it is not a run-time binding, but a static binding in general. So in the case of an upward transition, the object's method can find the subclass, while the object's attributes (member variables) or the parent class's properties (the subclass's shadowing of the parent member variable).

public class Father { 
  protected String name = "Father Property"; 
} 
 
public class Son extends Father { 
  protected String name = "Son attribute"; 
 
  public static void Main (string[] args) { 
    Father sample = new Son (); 
    System.out.println ("Invoked properties:" + Sample.name); 
  } 

conclusion, the members of the call are attributes of the father.

This result indicates that the object of the subclass (handle by reference to the parent class) is called the member variable of the parent class. So it's important to be clear that the scope of the runtime (dynamic) binding is just the object's approach.

Now trying to call the subclass's member variable name, what do you do? The easiest way to do this is to encapsulate the member variable into a method getter form.
The code is as follows:


public class Father { 
  protected String name = "Father attribute"; 
 
  Public String GetName () {return 
    name; 
  } 
} Public 
 
class Son extends Father { 
  protected String name = "Son attribute"; 
 
  Public String GetName () {return 
    name; 
  } 
 
  public static void Main (string[] args) { 
    Father sample = new Son (); 
    System.out.println ("invoked property:" + Sample.getname ()); 
  } 
 

Result: The property of the son is invoked

Java is a static binding method for attributes. This is because static binding has a lot of benefits, and it allows us to discover bugs in the program at compile time, not at runtime. This can improve the operation efficiency of the program! But the dynamic binding of the method is to realize polymorphism, polymorphism is a major feature of Java. Polymorphism is also one of the key technologies of object-oriented, so it is worthwhile for Java to realize polymorphism at the cost of efficiency.

Note: Most of the above is from the Internet, a small part is personal opinion, not authoritative speech. If there is a language inappropriate or incorrect expression of the place, Wan Wang advice.

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.