today dusted Thanking in java. Findings obtained through the class Class object literal references and Class.forName () method some differences. Special record.
1.Class.forName (String className):
classNamethis way must be used to pay attention to the fully qualified name (which includes the full package name)
2. class literal
example: FancyToy.class
advantages of doing so are: 1. compiler will examine it, no statement in a try block. So much simpler and safer.
2. eradicated right forName () method call, it is more efficient.
class literal can not only be applied to an ordinary class, but also can be applied to interfaces, arrays, as well as basic data types.
In addition to basic data typeswrapper, there is a standard TYPE field, which is a reference, namely, the basic data types to the corresponding Class object, which is equivalent to Boolean.TYPE boolean.class , void.class equivalent Void.TYPE so on. However, to note the difference between Boolean.TYPE and Boolean.class, is different.
3. is a very important distinction.
To use the class, you need to go through three steps:
1. loading: This is performed by the class loader's. This step will find the byte code, and from these to create a bytecode Class object.
2. link: the link stage will validate class bytecode for static fields allocation of storage space, and if necessary, will resolve this class creates all references to other classes.
3. Initialization: If the class has a superclass, then initialize it, perform static initializers and static initialization block.
forName () in this way completely after these three steps. But ". Class", and the third step will be delayed, the delay to a static method or static field for the first time very few references when executed, consider the following code:
package bells;
import java.util.Random;
/**
* @author bells
*
*/
public class TestClassInitialization {
public static Random rand = new Random(47);
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Class initable = Initable.class;
System.out.println("After creating Initable ref");
System.out.println(Initable.staticFinal); //这里Initable的static块不会被初始化,因为staticFinal是编译器常量
System.out.println(Initable.staticFinal2);
System.out.println(Initable2.staticNonFinal);
Class initable3 = Class.forName("typeinfo.Initable3");
System.out.println("After creating Initable3 ref");
System.out.println(Initable3.staticNonFinal);
}
}
class Initable {
static final int staticFinal = 47;
static final int staticFinal2 = TestClassInitialization.rand.nextInt(1000);
static {
System.out.println("Initializing Initable");
}
}
class Initable2 {
static int staticNonFinal = 147;
static {
System.out.println("Initaialzing Initable2");
}
}
class Initable3 {
static int staticNonFinal = 74;
static {
System.out.println("Initializing Initable3");
}
}
runs as follows:
can be seen from the results, use only. class syntax to get the reference to the class will not cause initialization. But Class.forName () will immediately be initialized.
Another point: If a static final value is "compile-time constant", like Initable.staticFinal so, this value does not need to be initialized Initable class can be read. Remember that it must be a compile-time constants job.
没有评论:
发表评论