Secrets behind Java

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.
[Java]
Public class {
Public void print (){
System. out. println ("Using class ");
}
}

Public class {
Public void print (){
System. out. println ("Using class ");
}
} [Java] view plaincopyprint? Public class B {
Public void print (){
System. out. println ("Using class B ");
}
}

Public class B {
Public void print (){
System. out. println ("Using class B ");
}
} [Java] view plaincopyprint? Public class Main {
Public static void main (String [] args ){
A a = new ();
A. print ();
B B = new B ();
B. print ();
}
}

Public class Main {
Public static void main (String [] args ){
A a = new ();
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:
[Java]
Public interface Office {
Void start ();
}

Public interface Office {
Void start ();
} [Java] view plaincopyprint? Public class Word implements Office {
Public void start (){
System. out. println ("Word start ");
}
}

Public class Word implements Office {
Public void start (){
System. out. println ("Word start ");
}
} [Java] view plaincopyprint? Public class Excel implements Office {
Public void start (){
System. out. println ("Excel start ");
}
}

Public class Excel implements Office {
Public void start (){
System. out. println ("Excel start ");
}
} [Java] view plaincopyprint? 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 ();
}
}

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 with 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.
[Java]
Public class Main {
Public static void main (String [] args) throws Exception {
System. out. println ("begin load ");
// Main 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 ();
}
}

Public class Main {
Public static void main (String [] args) throws Exception {
System. out. println ("begin load ");
// Main 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.


 

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.