Secrets behind Java 03

Source: Internet
Author: User

Java language is a inherently dynamic language. It is precisely because of the existence of Java class loaders that is dynamic. This will bring us a revolutionary change in development, because we may develop flexible and scalable programs. This greatly enhances the maintainability of our programs. The load conditions are divided into pre-loading and on-demand loading.
1. Basic class libraries usually need to be pre-loaded. (Resident memory)
2. The custom class library needs to be loaded as needed. (Loaded during use, and released by the garbage collector after use)

The classloader runs in two modes, for example:

1. Use the New Keyword for implicit loading. First, let's look at an example: directly add the code, three simple classes.
public class A {public void print() {System.out.println("Using class A");}}
public class B {public void print() {System.out.println("Using class B");}}
public class Main {public static void main(String[] args) {A a = new A();a.print();B b = new B();b.print();}}

The running result is as follows:

However, if we need to check the details of class loading, we need to use this command: Java-verbose: Class main

Check whether the base class library of Java is loaded first. Check the last few lines, first load main, and then load a when instantiating Class A, and call. using Class A is printed during print (), Class B is loaded, and the method in Class B is executed.

2. Use the class. forname () method to load the class. We should know that it can be loaded when the database connection is loaded with the driver. Let's look at a simple example:

public interface Office {void start();}
public class Word implements Office {public void start() {System.out.println("Word start");}}
public class Excel implements Office {public void start() {System.out.println("Excel start");}}
public class Main {public static void main(String[] args) throws Exception{Class c = Class.forName(args[0]);Object o = c.newInstance();Office office = (Office) o;office.start();}}

Continue to take a look at the loading details:

Let's take a closer look at the loading sequence of the last few lines and see if it is the same as what our program executes?
The conclusion is that the Java class loader loads classes sequentially from top to bottom according to the inheritance system until all the ancestor classes are loaded (interfaces in the above example; conversely, when Java class object instances are collected and released, the order is the opposite of loading. From this, we can also see that the inheritance system of Java classes should not be too long, because it will affect the performance of loading and releasing classes.

3. directly use the classloader. The example is a revision of the previous example, but the main. Java is modified, and the other three classes remain unchanged.

public class Main {public static void main(String[] args) throws Exception {System.out.println("begin load");// Main main = new Main();// ClassLoader loader = main.getClass().getClassLoader();ClassLoader loader = Main.class.getClassLoader();Class c = loader.loadClass(args[0]);System.out.println("begin init");Object o = c.newInstance();Office office = (Office) o;office.start();}}

First, obtain the classloader instance. There are two methods. One is commented out above, and the other is opened. Call the loadclass () method. Run the command to check the effect:

How about it? The Java language is dynamic enough.

Source> J2EE area-> Blue Mountains work-secrets behind Java (Summary)

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