Java getclass ()

Source: Internet
Author: User

Java Reflection Learning

The so-called reflection can be understood as the operation of obtaining the object type information during the run time. Traditional programming methods require programmers to decide which type to use during the compilation phase, but with the help of reflection, programmers can dynamically obtain this information to write more portable code. Strictly speaking, reflection is not a feature of programming languages, because the reflection mechanism can be implemented in any language, but the implementation of reflection is much more convenient if the programming language itself supports reflection.

1, Get type class

We know that in Java Everything is an object, and we generally use objects that are directly or indirectly inherited from the object class. the object class contains a method called GetClass, which allows you to obtain an instance of the type class. A type class refers to a class that represents a type, because everything is an object, and the type is no exception, and in Java the Type class is used to represent a type. All type classes are instances of class classes. For example, there is the following code:

A = new A ();

if (A.getclass () ==a.class)

System.out.println ("equal");

else System.out.println ("unequal");

The result is a print "equal".

You can see that the objectais aAan instance ofAA class, inifused in statementsA.getclass ()The returned result is exactlyAthe type class, inJavaindicates that a type class of a particular type can be used"type. Class"the way to get becauseA.getclass ()Get isAtype class, which isA.class, so the result of the above code execution is to print out"Equal". It is particularly important to note that the type class is one by one, and that the type class and subclass of the parent class are different, so supposeAis aBsub-class, the following code will get"Unequal"output of:

A = new A ();

if (A.getclass () ==b.class)

System.out.println ("equal");

else System.out.println ("unequal");

So, if you know an instance, then you can get the type class of the object through the "getclass ()" method of the instance, and if you know a type, you can use the " . Class " method to get the type class for that type.

2. Get the type of information

After you get the type class, you can call some of these methods to get the type of information, the main methods are:

GetName (): String: Gets the full name of the type.

Getsuperclass (): Class: Gets the direct parent of the type, and returns null if the type does not have a direct parent class .

Getinterfaces (): class[]: Gets all interfaces implemented by the type.

IsArray (): Boolean: Determines whether the type is an array.

Isenum (): Boolean: Determines whether the type is an enumeration type.

Isinterface (): Boolean: Determines whether the type is an interface.

Isprimitive (): Boolean: Determines whether the type is a base type, that is, int,boolean,double and so on.

IsAssignableFrom (Class CLS): Boolean: Determines whether this type is a Parent (Ancestor) class or parent (ancestor) interface of type CLS.

Getcomponenttype (): Class: If the type is an array, then the component type of the array is returned.

Java getclass ()

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.