Class classes in Java

Source: Internet
Author: User

      class is an implementation that defines a particular class in the Java language. The definition of a class contains the member variables, the member methods, the interfaces that the class implements, and the parent class of the class. The class class object is used to represent classes and interfaces in the currently running Java application. For example, each array belongs to a class class object, and all arrays that have the same element type and number of dimensions share a class object. The basic Java types (Boolean, Byte, Char, Short,int, long, float, and double) and void types can also be represented as Class objects.

The following example uses a class object to display the class name of an object:

 code   void  printclassname (Object obj) { 
System. out . println ("the class of " + obj + " is "+ obj.getclass (). GetName ()); }

We all know that all Java classes inherit the object class, and there is a method in the object class: GetClass (). This method is used to obtain a reference to the class of the object that the class has been instantiated, which points to the class object (hehe, a bit awkward). We cannot generate a class object ourselves (the constructor is private), and the class object is created by the Java virtual machine to automatically create the class object when the classes are called, or through the DefineClass method in the class loader. The object we generate will have a field that records the location of the object to which the object belongs in the class class. As shown in the following:

We can treat each class object as a proxy for many classes. And in each class object there will be a class loader with fields that record the class that he refers to. If the field is null, the loader for the class is bootstrap loader. For specific reasons see I previously shared a "work mechanism of ClassLoader " article.

We know that there are multiple loaders in Java, each loader can load multiple classes, so as long as the class object is obtained, its getclassloader () method can be used to obtain a reference to the ClassLoader.

The JVM is the only class object for each type of manager. So we can compare objects with the double equals operator: A1.getclass () ==a.class;

forname (String classname) and forname (stringclassname,{*}boolean initialze,*classloader *loader) * method.

The method returns the corresponding class object for the given string name. Given the full pathname of a class or interface, this method attempts to locate, mount, and connect to the class. If successful, returns the Class object. Otherwise, the classnotfoundexception exception is thrown. For example, the following code snippet returns a running class descriptor named Java.lang.Thread. Classt=class.forname ("Java.lang.Thread"); This method is required to specify the class loader when a forname method with only one string parameter is used. The class object will call the current classloader as the loader by default and the second parameter to True. The second parameter description: If False, calling the Forname method simply loads the class in the command class loader without initializing the static chunk of the class, only if the class is instantiated for the first time, the static chunk is called. When true, static chunks are called when they are loaded.

getClassLoader ()

Gets the class loader for this class.

Getcomponenttype ()

If the current class represents an array, the class object that represents the array component is returned, otherwise null is returned.

GetConstructor (class[])

Returns the specified public construction child object for the class represented by the current class object.

GetConstructors ()

Returns an array of all the public constructed sub-objects of the class represented by the current class object.

Getdeclaredconstructor (class[])

Returns the specified constructed child object for the class represented by the current class object.

Getdeclaredconstructors ()

Returns an array of all the described constructed sub-objects for the class represented by the current class object.

Getdeclaredfield (String)

Returns a Field object that specifies the specified description of the class or interface that the current class object represents.

Getdeclaredfields ()

Returns an array of all the described domain objects for the class or interface represented by the current class object.

Getdeclaredmethod (string,class[])

Returns the specified description of a method object for the class or interface represented by the current class object.

Getdeclaredmethods ()

Returns an array of all the described methods for the class or interface represented by the class object.

GetField (String)

Returns the specified public member domain object for the class or interface represented by the current class object.

GetFields ()

Returns an array of all accessible public domain objects for the class or interface represented by the current class object.

Getinterfaces ()

Returns the interface implemented by the class or interface represented by the current object.

GetMethod (string,class[])

Returns the specified public member method object for the class or interface represented by the current class object.

GetMethods ()

Returns an array of all public member method objects for the class or interface represented by the current class object, including the declared and inherited methods from the parent class.

GetModifiers ()

Returns the Java language modifier code for the class or interface.

GetName ()

Returns the full pathname string for the type represented by the class object (class, interface, array, or base type).

GetResource (String)

Finds a resource by the specified name.

getResourceAsStream (String)

Finds a resource with the given name.

Getsigners ()

Gets the class tag.

Getsuperclass ()

If this object represents any class other than object, the parent class object of this object is returned.

IsArray ()

Returns true if the class object represents an array, otherwise false.

IsAssignableFrom (Class)

Determines whether the class or interface represented by the class object is the same as the class or interface represented by the class specified by the parameter, or its parent class.

Isinstance (Object)

This method is a dynamic equivalence method for Java language instanceof operations.

Isinterface ()

Determines whether the specified class object represents an interface type.

Isprimitive ()

Determines whether the specified class object represents a base type of java.

Newinstance ()

Creates a new instance of the class.

ToString ()

Converts an object to a string.

Class classes in Java

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.