Understanding class Class and Its Application

Source: Internet
Author: User

I. Class
1. The class Object describes the running classes and interfaces. You can use the Class object to obtain information about the running classes and interfaces.
2. Each class has a corresponding Class object. The class Object of each Class is stored in the file where the compiled class is located. Therefore, when the JVM loads a. class file, it loads a Class object. Therefore, a class has only one Class object.
Ii. Basic Application of the Class
1. How to obtain a class Object of A Class
1.1 using the Class. forName (classname) method, this method receives a String parameter to specify the class Object of the Class to be generated, such as Class. forName ("Dog ").
1.2 It is obtained through class literals.
1) the literal constant is in the form of classname. class. For example, Dog. class.
2) for basic classes, each basic TYPE of overwriting Class has a standard data named TYPE, which can generate a reference pointing to the corresponding basic TYPE of Class Object. For example, int. class is equivalent to Integer. TYPE.
1.3 obtained through the Object. getClass () method, as shown in figure

  1. Dog dog =NewDog ();
  2. Dog. getClass ();

1.4 example of generating a Class Object
  1. ClassCat {
  2. Cat (){System. Out. println ("Init Cat ()");}
  3. Static{
  4. System. Out. println ("Loading Cat ");
  5. }
  6. }
  7. ClassDog {
  8. Dog (){System. Out. println ("Init Dog ()");}
  9. Static{
  10. System. Out. println ("Loading Dog ");
  11. }
  12. }
  13. ClassDuck {
  14. Duck (){System. Out. println ("Init Duck ()");}
  15. Static{
  16. System. Out. println ("Loading Duck ");
  17. }
  18. }
  19. Public ClassTest {
  20. Public Static VoidMain (String[] Args ){
  21. System. Out. println ("in main ()");
  22. NewCat ();
  23. System. Out. println ("After create Cat ()");
  24. Try{
  25. ClassC1 =Class. ForName ("Dog ");// (1)
  26. ClassC2 = Dog.Class;// (2)
  27. }
  28. Catch(ClassNotFoundExceptionCnfe ){
  29. Cnfe. printStackTrace ();
  30. }
  31. System. Out. println ("After Class. forName (" Dog ")");
  32. Duck d =NewDuck ();
  33. System. Out. println ("After create Duck ()");
  34. ClassC3 = d. getClass ();
  35. System. Out. println ("After Class. forName (" Duck ")");
  36. }
  37. }

The running result is:
In main ()
Loading Cat
Init Cat ()
After create Cat ()
Loading Dog
After Class. forName ("Dog ")
Loading Duck
Init Duck ()
After create Duck ()
After Class. forName ("Duck ")
Code (1) (2) generates the class Object of Class Dog, but does not generate the class Dog object.
**: Generating a class object does not produce a Class object.
2. Use Classc object for type comparison
2.1. Direct comparison
The two Class objects, whether compared using the equals () function or directly using the = Operator, compare whether they are of the same type.
2.2. Use the Class. isInstance (object) function for comparison, as shown in figure
  1. ClassCat {}
  2. ClassDog {}
  3. ClassDuck {}
  4. Public ClassTest {
  5. Public Static VoidMain (

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.