JAVA SE Basics Review-class and reflection

Source: Internet
Author: User
Tags java se

Class

For class, refer directly to http://lavasoft.blog.51cto.com/62575/15433

  Java programs run at runtime and the Java Runtime system always identifies all objects as run-time types. This information records the classes each object belongs to. A virtual machine typically uses run-time type information to perform the correct method, and the class that holds the type information is the classes class. The class class encapsulates the state of an object and an interface runtime, and when the class is loaded, objects of class type are automatically created.
Class has no public constructor method. Class objects are constructed automatically by the Java virtual machine when the class is loaded and by invoking the DefineClass method in the ClassLoader, so you cannot explicitly declare a class object.
Virtual machines manage a unique class object for each type. In other words, each class (type) has a class object. When you run the program, the Java Virtual Machine (JVM) first checks to see if the class object for the classes that you want to load is already loaded. If it is not loaded, the JVM will look up the. class file based on the class name and load its class object.
The basic Java types (Boolean, Byte, char, short, int, long, float, and double) and the keyword void also correspond to a Class object.
Each array belongs to a class that is mapped to a class object, and all arrays that have the same element type and number of dimensions share the class object.
A class object in general is loaded into memory, and it is used to create all objects of that class.

First, how to get the object of class? There are three ways to get it:
1. Call the GetClass () method of the object class to get the class object, which is also the most common way to produce a class object. For example:
MyObject x;
Class C1 = X.getclass ();
2. Use the class class's Static Forname () method to get the class object corresponding to the string. For example:
Class C2=class.forname ("MyObject"), the employee must be the name of the interface or class.
3. The third method of getting class type objects is very simple. If T is a Java type, then T.class represents the matching class object. For example
Class CL1 = Manager.class;
Class Cl2 = Int.class;
Class Cl3 = Double[].class;
Note: The class object actually describes only the type, and this type is not necessarily a class or interface. For example, the above int.class is an object of type class. For historical reasons, the GetName method of the array type returns a strange name. Ii. Common methods of class
1, GetName ()
A class object describes the properties of a particular class, the most commonly used method in the class class GetName returns the name of the entity (class, interface, array class, base type, or void) represented by this class object as a String. 2, Newinstance ()
Class also has a useful way to create an instance of the class, which is called newinstance (). For example:
X.getclass.newinstance () Creates a new instance of the same type as x. The Newinstance () method invokes the default constructor (no parameter constructor) to initialize the new object. If there is no parameterless constructor, a instantiationexception error is thrown. This is why you need to have a parameterless constructor when using the JSON library. 3, getClassLoader ()
Returns the class loader for this class. 4, Getcomponenttype ()
Returns the Class that represents the array component type. 5, Getsuperclass ()
Returns the class of the superclass representing the entity (class, interface, base type, or void) represented by this class. 6, IsArray ()
Determines whether this class object represents an array class. third, some of the use of class skills
1. forname and newinstance are used together to create an object based on the class name stored in the string. For example
Object obj = Class.forName (s). newinstance (); 2. The virtual machine manages a unique class object for each type. You can therefore use the = = operator to compare class objects. For example:
if (e.getclass () = = Employee.class) ...

Reflection

JAVA SE Basics Review-class and reflection

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.