static binding and dynamic Binding of Java methods (an explanation of the mechanism of the upward transformation)

Source: Internet
Author: User

One: Binding

Associating a method with the class/object in which it is located is called the binding of the method. Bindings are divided into static bindings (early binding) and dynamic bindings (late binding).

Two: Static bindings

Static binding (pre-binding) means that the method is known to belong to the class before it is run, and can be connected to the class at compile time to navigate to this method.

In Java, the final, private, static modified methods and constructors are statically bound, without the need for a program to run, without the need for a concrete instance of the object to know the specific contents of this method.

Three: Dynamic binding

Dynamic binding (late binding) means that, depending on the specific instance object, you can determine exactly which method is in the process of running the program.

Dynamic binding is an important factor in polymorphism, which is achieved through the method table: Each class is loaded into the virtual machine, the metadata is saved in the method area, including a thing called a method table, and a pointer to the method defined by this class is recorded in the table. Each table entry points to a specific method code. If this class overrides a method in the parent class, the corresponding table entry points to the new code implementation. The method inherited from the parent class is in front of the method defined by the child class.

the compilation and operation principle of dynamic binding statements : We assume Father ft=new Son ();  Ft.say (); Son inherits from father and rewrites say ().

1: Compile: We know that when you move up, the child class object is executed with the parent class reference, and you can invoke the overridden method of the same name in the subclass with the parent class reference. But you can't call the new method in the subclass, why?

Because during the compilation phase of the code, the compiler looks for matching methods in the method table of the type in the methods area by declaring the type of the object (that is, the type of the reference itself) (the best match method: The closest call to the parameter type), if any, is compiled. (This is found based on the declared object type, so here is the method table for finding the Father class, and the Father class method table is not a new method of the subclass, so it cannot be called.) )

The compile phase is to ensure the existence of the method, ensuring that the program can run smoothly and safely.

2: Run: We also know that Ft.say () calls the son of the Say (), this is not to say, find Father class method table matching method contradictory? No, this is the true embodiment of the dynamic binding mechanism.

The above compilation phase looks for methods in the method table that declares the object type, just to safely pass the compilation (and also to verify that the method is present). And in the actual run of this statement , in the execution of Father ft=new Son (); This sentence creates a son instance object, and then when the method is called by the Ft.say (), the JVM presses the son object into the operand stack and uses it to invoke it. The process of invoking a method with an instance object is dynamic binding: Find the method table of the instance object according to the type it belongs to, and locate the matching method to make the call. we know that if a method of the parent class is overridden in a subclass, the table item with the same name in the method table points to the method code of the subclass, and if no overrides, it is saved in the Child class method table in the order of the method table in the parent class. Therefore: Dynamic binding Method Table lookup methods based on the type of object are bound to match (because the compile-time in the parent class method table and the lookup and match succeeded, the method is present.) This also explains why the parent class reference cannot invoke the new method of the subclass when it is turned up: the existence of the method must first be tested in the parent class method table, and if it is tested at run time it is easy to be dangerous-there is no such method in the subclass.

Four: Distinguishing

When the JVM is running, the program will load the metadata of class type information, static property and method, final constant and so on to the method area, which is already known when the class is loaded, it can be accessed without object creation, it is statically bound content; The content that is dynamically bound is used when it is based on the type of the instance object in the heap.

static binding and dynamic Binding of Java methods (an explanation of the mechanism of the upward transformation)

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: 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.