I . Class Class
1. Class The object describes the running Classes And Interfaces . Pass Class Object can be obtained Classes And Interfaces . See examplesProgram
2. Each class has a corresponding Java. Lang. Class Object. The Class The object is stored in the file where the compiled class is located. Therefore, when JVM Load one . Class The file will load Class Object. Therefore, a class has only one Class Object.
II. How to obtainClassObject
1 . Pass Class. forname (classname) Method, which receives String Parameter to specify which Class Of Class Object . , Such Class. forname ("dog ") .
2. It is obtained by a literal constant of the class.
1) The literal constant is in the form: Classname. Class . For example Dog. Class .
2) For a basic class, each basic type has an override class named Type To generate a basic type Class Object Reference . For example Int. Class Equivalent Integer. Type .
3. Pass Object. getclass () Method.
ExampleSubroutine:
Class Dog
{
String name;
}
Class Test
{
Public Static Void Main (string [] ARGs) Throws Exception
{
Dog dog = New Dog ();
// An instance of the class indicates the classes and interfaces in the running Java application.
// The following statement is used to obtain the Running Dog class.
Class dogclass = Dog. getclass ();
String s = Dogclass. getname (); // S = dog
Package P = Dogclass. getpackage (); // Returns the package name of the Dog class.
Dog dog2 = (DOG) dogclass. newinstance (); // Creates a new instance (DOG) of the class represented by this class Object)
Dog2.name = " YY " ;
System. Out. println (dog2.name );
System. Out. println (dog. Name );
System. Out. println (s );
System. Out. println (P );
}
}