1 ImportJava.util.HashSet;2 ImportJava.util.Iterator;3 4 /**5 * Created by GOD on 2016/1/23.6 * The Class object is generated in the following way:7 1. Class Name: The JVM will use the class loader to load the class into memory (provided that the class has not been loaded into memory) and does not do the initialization of the class. Returns the object of class8 2.class.forname ("class name string") (Note: Class name string is package name + class name) Description: Load class and do static initialization of class, return class object9 3. Instance object. GetClass () Description: Static initialization of the class, non-static initialization; Returns a class object that references the object that the runtime really refers to (because: a reference to a child object may be assigned to a reference variable in the parent object)Ten */ One Public classClassandgetclass { A Public Static voidMain (string[] args)throwsClassNotFoundException { - - //Test. Class theClass Testtypeclass = Testclasstype.class; -System.out.println ("Testtypeclass---" +testtypeclass); -SYSTEM.OUT.PRINTLN ("Class load memory Complete"); - + //Test Class.forName () -Class testtypeforname = Class.forName ("Testclasstype"); +System.out.println ("Testtypeforname---" +testtypeforname); A at //Test Object.getclass () -Testclasstype Testtypegetclass =NewTestclasstype (); -System.out.println ("Testtypegetclass---" +Testtypegetclass.getclass ()); - -HashSet HashSet =NewHashSet (); -Hashset.add ("s"); inIterator Iterator =hashset.iterator (); - System.out.println (Iterator.getclass (). GetName ()); toSystem.out.println (Iterator.class. GetName ()); + - the * } $ }Panax Notoginseng - classTestclasstype { the //constructor Function + PublicTestclasstype () { ASystem.out.println ("----Constructor---"); the } + //Static parameter initialization - Static { $SYSTEM.OUT.PRINTLN ("---static parameter initialization---"); $ } - //non-static parameter initialization - { theSYSTEM.OUT.PRINTLN ("----non-static parameter initialization---"); - }Wuyi}
Output Result:
Testtypeclass---Class Testclasstype class loaded into memory---Static parameter initialization---testtypeforname---class Testclasstype----Non-static parameter initialization-------Constructor---testtypegetclass---class testclasstypejava.util.hashmap$ KeyIteratorjava.util.Iterator
The process of generating a class object is actually the same:
When we write a new Java class, the JVM will compile it into a class object, stored in a. class file of the same name. At run time, when the object of this class needs to be generated, the JVM checks to see if the class is already loaded in memory. If it is not loaded, the. class file is loaded into memory. If loaded, the instance object is generated according to the class file.
1 ImportJava.util.*;2 Importjava.lang.reflect.*;3 4 Public classCSDN {5 Public Static voidMain (String args[])throwsexception{6Set s =NewHashSet ();7S.add ("foo");8Iterator it =s.iterator ();9class[] Argsclass =NewClass[0];TenMethod m = Iterator.class. GetMethod ("Hasnext", argsclass); One System.out.println (M.invoke (It,argsclass)); A } -}
can be performed normally
1 ImportJava.util.*;2 Importjava.lang.reflect.*;3 4 Public classCSDN {5 Public Static voidMain (String args[])throwsexception{6Set s =NewHashSet ();7S.add ("foo");8Iterator it =s.iterator ();9class[] Argsclass =NewClass[0];TenMethod m = It.getclass (). GetMethod ("Hasnext", argsclass); One System.out.println (M.invoke (It,argsclass)); A } - } - //cannot execute
Reason Ibid .: System.out.println (Iterator.getclass (). GetName ()); Java.util.hashmap$keyiterator
System.out.println (Iterator. Class. GetName ()); Java.util.Iterator
Class names in Java. classes, instances. GetClass () Difference