Java dynamic linking

Source: Internet
Author: User

There are two types of binders in Java, one is dynamic and one is static.

Dynamic linking: Also known as polymorphic or late-linking, because which function to invoke is not determined at compile time and is deferred to run. That is, the type of object pointed to by the pointer is determined when the program is run.

Static linking: Static linking refers to the work of the binder in the compile connection phase, which is also known as the early link, because this kind of work is done before the program starts running.

Conditions for static Union: Class method (static method), class variable (static type variable).

EXM:

public class par_test{//base class

public static int i=4;

Public Par_test () {//constructor

System.out.println ("Par_test initialzed");

}

public void Write () {//Dynamic Union

System.out.println ("Par_test write" +i);

}

public static void Writestatic () {//static method test static Union

System.out.println ("Par_test writestatic" +i);

}

}

public class Chi_test extends par_test{//subclass

public static int i=5;

Public Chi_test () {//constructor

System.out.println ("chi_test initialized");

}

public void Write () {//Dynamic Union

System.out.println ("Chi_test write" +i);

}

public static void Writestatic () {//static method test static Union

System.out.println ("Chi_test writestatic" +i);

}

}

public class test{

public static void Main (string[] pars) {

Pra_test Test = new Chi_test (); Test initialization Constructor Order

Test.write (); Test dynamic linking

Test.writestatic (); Test Static Union

}

}

Program execution Results:

Par_test initialized;

Chi_test initialized;

Chi_test Write 5;

Par_test writestatic 4;

The results of the program run can be seen:

1. When constructing a subclass object, the constructor of its parent class is called before the class constructor is executed.

2, for the general method even if the object is coerced into the parent class object, the program runs the method in the subclass.

3, for a static method, because at compile time determines its order of execution, so the program run time to execute the static method of the object after casting.

Java dynamic linking

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.