Java. Lang. Class

Source: Internet
Author: User
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 );
}

}

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.