Java.lang.reflect.Field

Source: Internet
Author: User

Java.lang.reflect.Field

I. What is field class?
field is a class that is located under the Java.lang.reflect package.
In Java reflection, the field class describes the property information of a class, and in layman's terms there is a class like this:

1  PackageCom.testreflect;2  Public classFielddemo {3      Public intNUM1 = 1;4     protected intnum2 = 2;5     intNUM3 = 3;6     Private intNUM4 = 4;7     8      PublicString S1 = "a";9     protectedString s2 = "B";TenString s3 = "C"; One     PrivateString S4 = "D"; A}

Properties in the Fielddemo class in Java reflection: Num1, Num2, num3, and num4 are all instances of the field class, and an instance of the field class describes all of the properties ' information. (includes: Attribute name, property type, property modifier, attribute annotation, and so on)

Second, how to Get Field class object
There are 4 ways, all in class:
-GetFields (): Gets the properties of the public type in the class
-GetField (String name): Gets the class-specific method that specifies the name of the property
-Getdeclaredfields (): Gets all the properties in the class (public, protected, default, private), but does not include inherited properties.
-Getdeclaredfield (String name): Gets the class-specific method that specifies the name of the property

Iii. methods commonly used in field classes
For properties in a class, we use the following operations: Get the value of the property, modify the value of the property.

1  PackageCom.testreflect;2 3 ImportJava.lang.reflect.Field;4 ImportJava.lang.reflect.Modifier;5 6  Public classFieldtest {7      Public Static voidMain (string[] args)throwsException {8         //Use reflection First step: Get the Class object corresponding to the action class Fielddemo9Class<?> cls = Class.forName ("Com.testReflect.FieldDemo");Ten         //generating an instance using the class object of the Fielddemo classes OneObject obj =cls.newinstance (); A                  -         //GetField in class (String name): Gets the class-specific method that specifies the name of the property -Field field = Cls.getfield ("NUM1");  the  -         //Once you get the instance of the field class, you can call the method. -          -         //method: GetModifiers () returns the Java language modifier for the field represented by this field object as an integer +System.out.println ("modifier:" +modifier.tostring (Field.getmodifiers ())); -  +         //method: GetType () returns a Class object that identifies the type of claim for the field represented by this fields object ASystem.out.println ("type:" +Field.gettype ()); at          -         //method: Get (Object obj) returns the value of the field represented by this field on the specified object, obj -System.out.println ("attribute value:" +field.get (obj)); -          -         //Method: Set (Object obj, object value) sets the field that is represented by this field object on the specified object variable to the specified new value -Field.set (obj, 55); inSYSTEM.OUT.PRINTLN ("Modify attribute value--get (Object obj):" +field.get (obj)); -     } to}

Java.lang.reflect.Field

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.