Can the Java reflection mechanism dynamically modify the member variables of the final decoration in the instance?

Source: Internet
Author: User

Question: Can the Java reflection mechanism dynamically modify the member variables of the final decoration in the instance?

The answer is in two different cases.

1. When the final modified member variable initializes the value at the time it is defined, the Java reflection mechanism cannot dynamically modify its value.

2. When the final modified member variable does not have an initialization value at the time of definition, it can also be dynamically modified by the Java reflection mechanism.

Experiment:

1. when the final modified member variable is defined, the value is initialized

1  Public Class Person {2      Private Final String name = "Damon.huang"; 3       Public String GetName () {4           return  This . Name; 5      }6 }
1 ImportJava.lang.reflect.Field;2 3 /**4  * @authorDamon.huang5  *6  */7  Public classTestreflect {8 9      Public Static voidMainFinalstring[] args) {Ten         FinalPerson p =NewPerson (); OneSystem.out.println ("Original value:" +p.getname ()); ASystem.out.println ("--------Separate----------"); - Changeporperty (p); -     } the  -      Public Static voidChangeporperty (FinalPerson P) { -         Finalclass<?> CLZ =P.getclass (); -         Try { +             FinalField NameField = Clz.getdeclaredfield ("name"); -Namefield.setaccessible (true); +Namefield.set (P, string.valueof ("Huang.damon")); ASYSTEM.OUT.PRINTLN ("Reflection mechanism Modified field instance value:" +Namefield.get (P)); atSYSTEM.OUT.PRINTLN ("Reflection mechanism modified the value of the person instance:" +p.getname ()); -System.out.println ("--------Separate----------"); -}Catch(Finalnosuchfieldexception e) { - e.printstacktrace (); -}Catch(FinalSecurityException e) { - e.printstacktrace (); in}Catch(Finalillegalargumentexception e) { - e.printstacktrace (); to}Catch(Finalillegalaccessexception e) { + e.printstacktrace (); -         } the     } *}

The results are as follows:

Original value: Damon.huang--------Separate the value of the Modified field instance after the----------reflection mechanism Namefield.get (p) : Huang.damon reflection mechanism The value of the modified person instance p.getname (): Damon.huang--------Separate----------

Why is the value of P.getname () not modified?

The final type of data is automatically optimized during compilation, i.e. all places where the variable is used are replaced with constants. So the Get method is automatically optimized to return "Damon.huang" after compiling, instead of return this.name;

Can be passed String name = (null!=null? "). DDD ":" Damon.huang "); To prevent automatic optimizations at compile time. Refere to: The answer refers to the result, and you see that the value of the variable is dynamically modified.

Original value: Damon.huang--------Separate the value of the Modified field instance----------reflection mechanism: The value of the modified person instance Huang.damon reflection mechanism: Huang.damon--------Separate----- -----

2. When the final modified member variable does not have an initialization value at the time of definition

1  Public classPerson {2     Private FinalString name;3 4      PublicPerson () {5          This. Name = "Damon.huang";6     }7 8      PublicString GetName () {9         return  This. Name;Ten     } One}
Original value: Damon.huang--------Separate the value of the Modified field instance----------reflection mechanism: The value of the modified person instance Huang.damon reflection mechanism: Huang.damon--------Separate----- -----

The result is that the final modified member variable value is dynamically modified

Can the Java reflection mechanism dynamically modify the member variables of the final decoration in the instance?

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.