Introduction to Java Class class

Source: Internet
Author: User
Tags reflection

What is the use of class classes?

Instances of class classes represent Java application Run-time classes (class ans enum) or interfaces (interface and annotation) (each Java class runtime behaves as a class object in the JVM, through class name. class, Type. GetClass (), Class.forName ("Class name") to get the class object. The array is also mapped to a class for the class object, and all arrays that have the same element type and number of dimensions share the class object. The basic type boolean,byte,char,short,int,long,float,double and the keyword Void are also represented as class objects.

Second, the class characteristics
The class class does not have a public constructor, which is invoked automatically by the JVM (when the new object or load-classloader).

The following method works by printing out the object's class name:

void Printclassname (Object obj) {
System.out.println ("The Class of" + obj +
"Is" + obj.getclass (). GetName ());
}
You can also get class name according to class literal:
System.out.println ("The name of Class Foo is:" +foo.class.getname ());//You can change Foo to void.
Three, the main method of class

Class classes are still quite a few ways to do it. It is primarily used to obtain information about Run-time classes (which can be used for reflection).

Several important methods are:

1, public static class<?> forname (String className): Natice method, dynamically loading classes. Very important.
such as dynamically loading drivers in sql: Class.forName (sqldriver);

2,public T newinstance (): Creates a new object for reflection based on the class of the object. Very important.
You can build objects in reflection and invoke object methods:

Class doubleclass= Class.forName ("java.lang.Double");

Object objdouble = Doubleclass.newinstance ();

This method is applied in JavaBean, because Java defaults to a parameterless constructor.

3, Public ClassLoader getClassLoader (): Gets class loader bootstrap, Extension, system or user custom ClassLoader (typically system CL Assloader). Important.

4,public String getName (): Gets the name of the class or interface. Remember that the enum is a class and annotation is an interface. Important

5,public Native Class Getsuperclass (): Gets the parent class of the class, inherits the parent class, returns the parent class, or returns Java.lang.Object. The parent class that returns object is an empty-null. So so
6,public Java.net.URL getresource (string name): A resource is obtained from a string.

7, other classes

public boolean isenum (): Determines whether the enumeration type is.

Public native Boolean IsArray (): Determines whether an array type.

Public native Boolean isprimitive (): Determines whether it is a basic type.
public boolean isannotation (): Determines whether the annotation type is.


Public Package getpackage (): Package is obtained in reflection, such as Package for Java.lang.Object.

public native int getmodifiers (): Get modifiers in reflection, such as public static void.

public field GetField (String name): Gets a domain member in reflection.

Public field[] GetFields (): Gets a domain array member.
Public method[] GetMethods (): Get method.

Public method Getdeclaredmethod (String name, Class<?> ... parametertypes): Add a declared to represent this class, inheritance, and the parent class are not included.

Public constructor<?>[] GetConstructors (): Gets all constructors.

So we know that reflection can dynamically get all the information about the class and create a new object (Newinstance () method) when it runs.

As we define a class:

public class test{

Constructor

Public Test () {This ("");}

Public Test (String name) {}

Field

public int id;

public String name;

Method

public void TestMethod () {

}

}
We can:

Class C = class.forname ("Test");
Method m[] = C.getdeclaredmethods ();
for (int i = 0; i < m.length; i++)
System.out.println (M[i].tostring ());//Output TestMethod
}

Constructor c[] = Cls.getdeclaredconstructors ();
for (int i = 0; i < c.length; i++) {
Constructor ct = c[i];
System.out.println ("name =" + Ct.getname ());//output two constructor information

No time to write, others use the computer ....

Related Article

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.