Java's class load principle (1)

xiaoxiao2021-03-06  47

First, what is the type of load in the Java class actually copies the .class file on the hard disk to the memory so that it can be called from memory when it is used; Java is loaded into the load. Class; class loader is part of JRE;

Second, the class load classification class has two ways: 1. Pre-load: For example, the Java class below rt.jar; preloaded the class below this package when running the Java program, because they are Java normal Running the basic class, which can save time after loading; 2. Load according to requirements: Generally, our own class is loaded according to demand; three, explicit load and implicit load class

When instantial, the class is implicitly loaded, for example, instantiation A a = new a () for A.CLASS; then implicitly loaded A.Class; but if it is just Declaration, without instantiation, for example: a a; this time does not load a.class. When loading the class, if there is a parent class, first load the parent class of the class; if the parent class of the class also exists, then the parent class of the parent class first, so on, The first thing to load is the uppermost class; in addition, it can also be explicitly loaded; there are two ways to explicitly load classes, one is a Forname method through java.lang.class; the other is LoadClass method through java.lang.classloader;

Load class: class c = class.forname (a); // Load C class; object o = c.newinstance (); // instantiate A A = (a) O;

The final call is a private static native method forName0 (String Name, Boolean Initialize, ClassLoader Loader) This method is the first parameter. If the second parameter is false, then when the class is loaded, it is not initialized static Block; when instantiated, the static block is initialized; when TRUE, the static block is initialized when the class is loaded; forname (a); actually calls forname0 (A, True, ClassLoader) getCallerClassLoader ());

转载请注明原文地址:https://www.9cbs.com/read-67147.html

New Post(0)