1. Class object
"Class object" represent the type information of java.
Class object is used to create all "regular" objects.
All class has a "Class object", JVM use class loader to load "Class object" to create class.All "Class objects" belongs to "class Class".
2. class Class
"class Class" with definition "Class <?>" is a "class tmeplate".
"Class object" is the object of "class Class"
3. class Class's main method
forNamenewInstance
4.How to get "Class object" of a class
a. ClassforName("classname");
b.Name.class
for example Class Dog
ClassforName("Dog");Dog.class;
5. judge the class of a object
isInstanceof
6.class object of Primitive type
boolelan.class=Boolean.TYPE
char.class=Character.TYPE
byte.class=Byte.TYPE
short.class=Short.TYPE
int.class=Short.TYPE
long.class=Long.TYPE
float.class=Float.TYPE
double.class=Double.TYPE
void.class=Void.TYPE
7.other
a.get length of array
b.Class classes[]; classes.length;
c.public class, class name must be the same as filename;
TestCode:
import java.lang.reflect.Constructor; class Shape{ } class Triangle extends Shape{ } public class ClassTest { public Shape shape; public Triangle triangle; public ClassTest(){ } public ClassTest(int i){ } public static void main(String[] args) throws ClassNotFoundException, SecurityException, NoSuchMethodException, NoSuchFieldException{ } }
Constructor constructors[] = Class.forName("elfylin.test.ClassTest").getConstructors(); for (int i = 0; i<constructors.length; i++){ System.out.println(constructors[i].toString()); }public elfylin.test.ClassTest()
2.getConstructor
Constructor constructor = Class.forName("elfylin.test.ClassTest"). getConstructor(int.class); System.out.println(constructor.toString());public elfylin.test.ClassTest(int)
3. getDeclaredFields
Field fields[] = Class.forName("elfylin.test.ClassTest").getDeclaredFields(); for (int i = 0; i<fields.length; i++){ System.out.println(fields[i].toString()); }public elfylin.test.Shape elfylin.test.ClassTest.shap
4.getDeclaredField
Field field = Class.forName("elfylin.test.ClassTest").getDeclaredField("triangle"); System.out.println(field);public elfylin.test.Triangle elfylin.test.ClassTest.triangle
5.getDeclaredMethods
Method methods[] = Class.forName("elfylin.test.ClassTest").getDeclaredMethods(); for (int i = 0; i<methods.length; i++){ System.out.println(methods[i].toString()); }public static void elfylin.test.ClassTest.main(java.lang.String[]) throws java.lang.ClassNotFoundException,java.lang.SecurityException,java.lang.NoSuchMethodException,java.lang.NoSuchFieldException
will also return field of it's parent
7.getMethods()
will also return method of it's parent/