Dynamic binding vs Static Binding

Source: Internet
Author: User

Dynamic bindings (aka late binding)

Dynamic binding means that the compiler does not know which method to invoke at compile time until the runtime can determine it. Let's use an example to explain. For example, we have a parent class called ' superclass ' and a subclass ' subclass ' that inherits it. Now superclass references can also be assigned to objects of type subclass. If there is a SomeMethod () method in superclass, and the subclass overrides this method, the compiler does not know whether to call the parent class or the subclass method when invoking the method referenced by superclass, because the compiler does not know what type the object is, You will only know what object this reference is pointing to at run time.

1 2 3 4 5 6 7 8 ... Superclass SuperClass1 =newsuperclass (); Superclass SuperClass2 =newsubclass (); ...   superclass1.somemethod ();//superclass version is called Superclass2.somemethod ();//Subclass version is Calle D ....

We can see that although object references SuperClass1 and superClass2 are superclass types, they point to objects of type superclass and subclass at run time.

So during the compile phase, the compiler is not sure whether invoking the referenced SomeMethod () is the method that invokes the subclass or the parent class.

Therefore, the dynamic binding of a method is based on the actual object type, not the object reference type that they declare.

Static binding (aka pre-binding)

If the compiler can complete the binding at compile time, it is called a static binding or a pre-binding. Basically, instance methods are bound at run time, and all static methods are bound at compile time, so static methods are statically bound. Because static methods are methods that belong to classes and can be accessed through the class name (we should also use the class name to access static methods rather than using object references), it is necessary to use compile-type information for binding at compile-time to access them. This also explains why static methods cannot actually be overridden.

Read more: Can I override static methods?

Similarly, access member variables are statically bound because Java does not support the polymorphic behavior of member variables (which are actually discouraged). Let's look at an example:

1 2 3 4 5 6 7 8 9-Ten classsuperclass{publicstring somevariable = "Some Variable in Superclass"; ...}   CLASSSUBCLASSEXTENDSSUPERCLA ss{publicstring somevariable = "Some Variable in Subclass"; .....   Superclass SuperClass1 =newsuperclass (); Superclass SuperClass2 =newsubclass ();   SYSTEM.OUT.PRINTLN (superclass1.somevariable); System.out.println (superclass2.somevariable); ...

Output:

1 2 Some Variable in superclass Some Variable in superclass

We can find that the member variables are determined by the type of the object reference declaration, which is the information that the compiler knows at compile time, so it is a static binding. Another example of a static binding is a private method, because they are not inherited, and the compiler completes the binding of the Private method during the compilation phase.


Dynamic binding vs Static Binding

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.