The duplicate name of the inheriting class from Java a brief talk on parsing call and dispatch

Source: Internet
Author: User

The intern asked this question today:
In Java, can static member functions be overridden?

The conclusion is that you can override a static function in a subclass, but this function does not run as a normal non-static function.

In other words, although you can define an overriding function, the function has no polymorphic properties. Let's Test it:

 1 class  testClass1{ 2     static void SMethod(){ 3         System.out.println("static in testClass1"); 4     } 5 } 6 class testClass2 extends testClass1{ 7     static void SMethod(){ 8         System.out.println("static in testClass2"); 9     }10 }11 public class MainClass{12     public static void main(String... args){13         testClass1 tc1=new testClass2();14         testClass2 tc2 =new testClass2();15         tc1.SMethod(); //输出结果为 static in testClass116         tc2.SMethod(); //输出结果为 static in testClass217     }18 }

As you can see from the results, the static function of the parent class is called when we call the static function with the instance reference of the parent class, which is actually a subclass of the instance.

The reason is the order in which the methods are loaded.

When a method is called, the JVM first checks that it is not a class method. If so, the method is found and executed directly from the class that called the method reference variable, and is no longer determined whether it is overridden (overwritten). If not, do other things (such as dynamic method queries)

Some people may have a pat on the thigh, this is not the static/dynamic allocation of Java!

A bit like, but not really, static dispatch and dynamic dispatch are used to determine overloading and overriding logic. During overloading, the compiler depends on the static type of the method parameter (such as TC1 's static type is CLASS1,TC2 Class2, but this article is not overloaded!). ) to determine the version of the method used, which is called static dispatch. Dynamic dispatch is used for method overrides, such as I call a method F of Class A, if the class has subclass a, then I call F with a, the actual call is A.F instead of A.F.

It really looks like a dynamic assignment, doesn't it? But the results do not match Ah!

The reason for this is that we are actually discussing the behavior of the Java invokevirtual instruction in dynamic dispatch: This instruction first looks for the caller's runtime type, and then finds the matching method in its method table, if it is not found, and then finds it from its parent class. This process is the essence of method rewriting in Java, which is dynamic dispatch.

The static method is called by the invokestatic directive. Because the static method is a compile-time known, the runtime immutable method, so although the subclass and the parent class have the same method name, and in fact they are different methods, but also can be completely differentiated method. When the static method is called, the compiler resolves its symbolic reference directly to a direct reference when the class is loaded, without saying that the subclass cannot find the method and then goes to the parent class to find the behavior, so it is called a parse call.

This is why the above example looks like an overridden method without producing a rewrite effect.

Complete the full text.

The duplicate name of the inheriting class from Java a brief talk on parsing call and dispatch

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.