Home  >  Article  >  Java  >  07.Java Basics - Static Binding & Dynamic Binding

07.Java Basics - Static Binding & Dynamic Binding

黄舟
黄舟Original
2017-02-27 10:29:531200browse

Basic concepts

Binding refers to the association of a method call with the class in which it is located.

Binding can be divided into static binding and dynamic binding.

Several concepts you need to know before analyzing static binding and dynamic binding:

  • Compilation period: The compilation process is to convert the Java source The process of compiling files into bytecode (.class file, JVM executable code). During this process, Java does not deal with memory. During this process, the compiler will perform syntax analysis. If the syntax is incorrect, an error will be reported. .

  • Running period: The running process refers to the JVM (Java virtual machine) loading the bytecode file and interpreting it for execution. This process is the real creation of memory. Execute Java program.


Method calling

The method calling process in Java is as follows:

  • Editor View the object's declared type and method names. Get all candidate methods that may be called due to method overloading. For example: method one is print(String str), method two is print(int).

  • The compiler checks the input parameter type of the calling method. Pick the matching method from the candidate methods. For example, if the input parameter is "hello", select print(String str).

  • If the method is private, static, final, or constructor, the compiler can determine which method to call. This is static binding.

  • If this is not the case, runtime (dynamic) binding must be used.


Static binding

Static binding, also known as early binding and compile-time binding. Indicates binding at compile time, that is, the method has been bound before the program is run.

Only methods, member variables, and constructors modified by final, static, and private are statically bound:

Type Explanation
final The method modified by it can be inherited, but cannot be overridden; subclass objects can be called, but what is called is The method defined in the parent class; indirectly indicating that declaring the method as final can avoid rewriting and turn off dynamic binding.
private The method modified by it implicitly contains the final keyword. Because it is invisible to the outside world, it cannot be inherited or overridden; it can only be called through the object of the class itself, so the object can be clear before the method is run.
static Static methods depend on classes and depend on objects. It can be inherited by subclasses (essentially hidden by subclasses), but cannot be overridden by subclasses. When a subclass object is transformed up to a parent class object, the object will use the static method in the parent class regardless of whether the static method is defined in the subclass. So here it is said that static methods can be hidden.
Member variables By default, Java uses static binding for properties, so that program errors can be discovered during compilation and can provide efficiency.
Construction method The construction method cannot be inherited. When a subclass inherits a parent class, it must first call the parent class's construction method (whether explicit or implicit). Therefore, you can know which object the constructor method refers to before the program is run.

Look at the following example:

// 父类class Parent{    // 变量
    String name="Parent";    // 静态方法
    static void print(){
        System.out.println("Parent print");
    }    // 私有方法
    private void say(){
        System.out.println("Parent say");
    }    // 终态方法
    final void look(){
        System.out.println("Parent look");
    }
}// 子类class Son extends Parent{
    String name ="Son";    static void print(){
        System.out.println("Son print");
    }    // 编译错误,无法重写父类的 final方法
    final void look(){};
}public class Test{
    public static void main(String[] args) {        // 发生向上转型
        Parent p = new Son();        // 输出 Parent
        System.out.println(p.name);        // 输出 Parent print
        p.print();        // 编译错误,对外不可见
        p.say();
    }
}

Dynamic binding

Dynamic binding , also known as late binding and runtime binding; it means binding according to the type of the specific object at runtime.

The process of dynamic binding:

  • The virtual machine extracts the method table of the actual type of the object;

  • Virtual machine search Method signature;

  • Call method.

Let’s look at an example:

class A {    int x = 5;
}

class B extends A {    int x = 6;
}

class Parent {    public A getValue() {
        System.out.print("Parent Method ");        return new A();
    }
}

class Son extends Parent {    public B getValue() {
        System.out.print("Son Method ");        return new B();
    }
}public class Test {
    public static void main(String[] args) {        // 向上转型
        Parent p = new Son();        // 输出结果:Son Method 5
        // 注意:是 5 不是 6 !
        System.out.println(p.getValue().x);
    }
}

Observe the output analysis as follows:

  • p.getValue(), due to There is an upward transformation, so it first looks for the method from the subclass (Son), and at this time it calls Son's method. This is dynamic binding.

  • p.getValue( ).x, since x is a member variable, its object (belonging to Parent) is determined before the program is run. Static binding occurs here.

If you still don’t understand, let’s look at an example:

class Parent {
    String name = "Parent " + this.getClass().getName();
}

class Son extends Parent {
    String name = "Son" + this.getClass().getName();
}public class Test {
    public static void main(String[] args) {        // 向上转型
        Parent p = new Son();        // 输出:Parent Son
        System.out.println(p.name);
    }
}

Observe the output and analyze it as follows:

  • p.name : name is a member variable, static binding occurs at this time, so the property of Parent is called.

  • this.getClass( ): getClass is a method. Since an upward transformation occurs at this time, the default program will search for this method starting from the subclass, which happens to also exist in the subclass. Therefore, the method of the subclass is called, and dynamic binding occurs at this time.


The above is the content of 07.Java Basics - Static Binding & Dynamic Binding. For more related content, please pay attention to the PHP Chinese website (www. php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn