statement: The summary of the article, from many netizens excellent blog
About accessing the class attribute through the name of the classes, my friend has been a few times, never understand what this thing is? In this respect, I refer to netizens blog, summed up some small knowledge, such as found errors, hope to correct, thank you
In fact, any class, there will be a class object in this category corresponding, in this class object, save the basic information required to instantiate the class, A.class actually returned a class A of the class object, paste a small code to demonstrate:
public class Test {/** * @param args */public static void main (string[] args) {//TODO auto-generated method stub//Get the class name (not Contains the package name) System.out.println (String.class.getSimpleName ());//string//Get the full name of the class (contains the package name) System.out.println ( String.class.getName ());//Java.lang.StringSystem.out.println (Test.class.getName ());//Test}}
How to get CALSS objectsThere are three main types:
The first is obtained by Class.forName ("Full name of the class"), which is most commonly used when used in the JDBC registration driver.
try {class.forname ("oracle.jdbc.driver.OracleDriver");//Register driver} catch (ClassNotFoundException e) {//TODO Auto-generated catch Blocke.printstacktrace ();}
The second type: obtained by an already instantiated object, the GetClass () method gets
Third: Get through class name. class
public class Test {/** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubtest Test = new Test (); System.out.println (Test.getclass ());//Class TestSystem.out.println (Test.class);//Class Test}}
Also on the Netizen blog to see a relatively good code: paste it
import java.util.*; classinitable{StaticFinalintstaticfinal= -; StaticFinalintStaticFinal2=ClassInitialization.rand.nextInt ( -); Static{System. out. println ("initialization initable"); }}classinitable2{Static intstaticnofinal= 147; Static{System. out. println ("initialization Initable2"); }}classinitable3{Static intstaticnofinal= About; Static{System. out. println ("initialization Initable3"); }} Public classclassinitialization { Public StaticRandom Rand= NewRanddom ( -); Public Static voidMain (string[] args) {Class initable=initable.class; //does not cause initializationSystem. out. println ("After creating initable reference"); System. out. println (initable.staticfinal);//referencing compiler constants does not cause initializationSystem. out. println (INITABLE.STATICFINAL2);//cause initializationSystem. out. println (initable2.staticnofinal);//referencing a non-compile-time constant causes initializationClass Initable3=Class.forName ("Initable3"); //initialization is caused by defaultSystem. out. println ("After creating Initable3 reference"); System. out. println (initable3.staticnofinal);//The previous initialization is no longer initialized here}}
If a static final value is a compile-time constant, a reference to the value does not have to be initialized for its class, and if it is only static and final but not a compile-time constant, it will be thrown to initialize its class. If a static value is referenced, it is linked and initialized with the class it resides in.