Behavior of the instance variable private & Final while working with reflection

Using reflection, I am able to successfully access the private members of the class A suite help extract:
package com.test;

public class A {

     
     private int _count = 0;
     public A() {
          
     }
     
     public void setCount(int count) {
          if ( count < 0){
               System.err.println("Value must be positive.");
               return;
          }
          _count = count;
     }
     
     public int getCount() {
          return _count;
     }
     
}
package com.test;

import java.lang.reflect.Field;

public class MainClass {

     private static Field f = null;
     A a  = null;
     static {
          try {
               
               f = A.class.getDeclaredField("_count");
               if ( f != null)
                    f.setAccessible(true);
               
          } catch (NoSuchFieldException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } catch (SecurityException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
          
     }
     
     
     public MainClass() {
          
          a = new A();
          
          if ( f != null ) {
               try {
                    f.set(a, -99);
               } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               }
          }
          
     }
     
     public void printCount() {
          
          System.out.println("Total Count : " + a.getCount());
          
     }
     
     public static void main(String[] args) {
          
     
          MainClass cls = new MainClass();
          cls.printCount();
          
          
     }
     
}
The above code works fine, but when I tried the same thing with the class provided as java.lang.String and tried access private member of this one, he threw an exception to access. If access can be restricted by setting SecurityManager I've tried in my case also and worked, but also in java.lang.String, I was unable to find any implementation for the SecurityManager who refused access to private members, then how is it that I'm not able to access by using reflection?

Also in the same example above, when I did a final variable _count, and tried changing the value by using reflection, that he did not throw any exception when running, but don't not change either value.

Appreciate any help on the improvement of the understanding on this.

Thanks in advance.

I gave it a try with Java 1.7.0_21 - b11.

import java.lang.reflect.*;

class Y {
     public static void main(String[] args) throws Exception {
          String s = "123";
          System.out.println(s);
          Field stringValue = String.class.getDeclaredField("value");
          stringValue.setAccessible(true);
                //stringValue.set(s, "321");
          stringValue.set(s, new char[]{'3', '2', '1'});
          System.out.println(s);
     }
}

Output:

123
321

Jtahlborn is right and your code may not agree. You may have seen:

Exception in thread "main" java.lang.IllegalArgumentException: Can not set final [C field java.lang.String.value to java.lang.String
        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
        at sun.reflect.UnsafeQualifiedObjectFieldAccessorImpl.set(Unknown Source)
        at java.lang.reflect.Field.set(Unknown Source)
        at Y.main(Y.java:8)

What is happening with the commented line.

Published by: baftos on May 16, 2013 15:07

Tags: Oracle

Similar Questions

  • I don't see the 'design' button while working with fluid layout, why?

    While working with the fluid RWD layout in dreamweaver:

    (1) I don't see the 'design' button, I can only see buttons ' Code, Split and living ", in Dremweaver CC, how can I get the preview of this design button?

    (2) and "Visual AIDS" is disabled, I mean I can't see the 'container' visually when you work in direct mode. So am not in a position to determine when and where to place the div

    Re: Is it possible to display the design view in the fluid grid pages?

  • I'm a software tester for an internal application to my company. version 10.0.2 is not cmopatible with a few controls within our software. I need the download v9.0.1 whatever works with the controls. Where can I get?

    I'm a software tester for an internal application to my company. version 10.0.2 is not cmopatible with a few controls within our software. I need the download v9.0.1 whatever works with the controls. Where can I get?

    Well, if you need older versions of firefox you can find them on this site:
    - http://www.oldapps.com/firefox.php

    Best regards, G-Cloud.

  • What is the latest version of Firefox, which works with Windows ME?

    We recently had a portable older Gateway that we use only as an internet point. Unfortunately, it is currently running Windows ME and I don't even know if it has all the technical features to be upgraded (and certainly not for Vista, which is the only thing that we have an installation disc for). The vehicle currently has Firefox 2.0.0.20, which of course contains many security holes and is not compatible with most of the newer plugins. What is the best version of Firefox that works with Windows ME, and it is still available for download? Thank you!

    Sorry, Firefox 2.0.0.20 is the last version made for WinME.

  • The "PNY QUADRO K4200" graphics card works with first? Maybe you suggest me another graphics card?

    The "PNY QUADRO K4200" graphics card works with first? Maybe you suggest me another graphics card?

    This card should work as its on the system requirements | Adobe Premiere Pro

    But first is not take advantage of the advanced features of quadro, and a similar performance quadro vs gtx can cost 5 times more. If you better save your money and go with a gtx. the gtx 950 would be about the same performance as this K4200, but much less expensive. a 970 gtx about 2 x more powerful, but still half the price.

  • (correct) different behavior when a shared variable IO node is replaced with programming

    I work with the following system:

    LV2010 running in Windows 7 and a cRIO-9073

    My RT and FPGA code is generated and deployed.  I have a pair of variables published network hosted and deployed on the cRIO.  I wrote a simple application for my PC save variables of network in the disc.  The vi running on my PC uses the variables nodes that were driven from the project window and are configured as "absolute".  When I run this logging vi he told me that variables are part of a startup RT application and the system must be restarted.  It makes no sense to me - they are hosted and already deployed on the cRIO.

    When I right click on both variable nodes and replace them with "programmatic access" (and do NOTHING ELSE), I can run this VI without any problem.  It "clearly a bug here or I don't use node IO variable shared properly."  Any ideas?

    Thank you!

    Ethan

    In your project, right-click on desktop and select 'Disable Autodeploy Variables'

  • While working with Office Home Student 2007 & microsoft it has locked and now I can not in it. He asked the product key that I entered re - nothing doesn't.

    How to unlock the program?

    Hello

    Check with the Office of the Experts here: (re - ask your question in these groups)

    Office newsgroups
    http://www.Microsoft.com/Office/Community/en-us/FlyoutOverview.mspx

    Discussions in the Office Setup program
    http://www.Microsoft.com/Office/Community/en-us/default.mspx?DG=Microsoft.public.Office.Setup&lang=en&CR=us
    Or

    Welcome to Microsoft Discussion groups
    http://www.Microsoft.com/communities/newsgroups/list/en-us/default.aspx?GUID=1A61081E-1F66-5F7F-B5BA-04767E55A63B

    Microsoft.public.office.setup discussions
    http://www.Microsoft.com/communities/newsgroups/list/en-us/default.aspx?DG=Microsoft.public.Office.Setup&cat=en_us_642d5640-c1ba-43C3-A224-b3ec1473346c&lang=en&CR=us

    I hope this helps.
    Rob - bicycle - Mark Twain said it is good.

  • Disable the update of the widget menu when you are working with files from Muse?

    First of all thank you Adobe for the creation of Muse - I really like it a lot!

    My question:

    When I work on my site with over 250 pages, he strains the performance to make even the smallest changes in the structure of the page. I work with two masters of every much the same horizontal menu widget.

    Add, remove, and reorganization of pages makes the PC (Windows 7 laptop to some but not impressive power) to stop and take a deep breathe for a few seconds. It really interrupts the workflow.

    A raw work around this is to remove the menu widget in the two masters and put it in a new master page that contains the copied menu only two other masters and keep the rest of the header in place on the masters originals to be used for the alignment on the pages. In this way, what a page is updated when you work with adding, deleting, and the reorganization of pages.

    I tried to find a preference disable the update of the widget Menu when working on the structure of the page and just allow the update again when the work is done? is - it? otherwise I would recommend it highly :-) Since my work is about a little difficult/risky to use after the publication of ;-)

    Again, thank you very much for this beautiful program. Being a user of CS (now CCC) selftought I like the ease of control of the Web design you can get with Muse (to the distribution unit other features of course - if you ever wanted to add some sort of database search feature it would be MUCH appreciated.) A workaround is to use the iframe and an external search/lookup page, which returns results with links to parent pages. Un_peu funny good construction :-) ).

    BR

    Mr. harder

    Glad to know you like Muse.

    It can be very intensive CPU if you have several Master pages with their own menu widget Pages. Take a look at the following threads and refer to the response of Zak.

    http://forums.Adobe.com/thread/1423767

    http://forums.Adobe.com/message/6166722

    See if you are able to use one menu widget for all Pages in your site while enjoying the function hierarchical master page.

    http://TV.Adobe.com/watch/Muse-feature-tour/Adobe-Muse-hierarchical-master-pages/

    We know the performance problems caused by the widget Menu (all types of pages) and is something being considered by engineering in one of the upcoming releases.

    Thank you

    Vinayak

  • one by the xerox phaser 7300 printer will work with El captain

    One by the xerox phaser 7300 printer to become compatible with the captain.  Xerox shows no current drivers available?

    I think that the support dropped for Phaser 7300 Xerox about 10.6.8. If the generic postscript driver is not good enough for you consider trying the Gutenprint drivers:

    http://GIMP-print.sourceforge.NET/MacOSX.php

    I don't have access to a Phaser 7300 so I can't comment on the way in which these solutions work, but IME, Gutenprint drivers are usually pretty good.

    C.

  • 19 of Firefox does not crashes not with the MSN homepage, Msn home page works with IE 9 well?

    I always put MSN as my homepage with all versions of Firefox. Firefox has suddenly stopped working (crashed 3 days ago). I had been running Firefox 19 a few days with no problem, simply ceased to work with MSN and just frozen screen? I tried MSN with Internet Explorer and no problems? I tried to disable the plug-ins and Java, but no use? The only way I can use Firefox is to use a different homepage, then there is no problem.

    iceman21, thanks for posting back. I'm glad that you have solved your problem.

    I suspect something may have changed, but still I have not discovered what could have any relevant change. A way or an another most users seem to be able to solve the problem, usually by disabling cookies and cache.

  • What is the latest version of firefox that works with albine 0,748?

    My albine worked with the last version before I upgraded to 17. I have not spared this version and I would like to return. I save firefox 9 and this is what I use now, but I think that you have a newer version that I could use.
    What was the version prior to 17 and where can I find it?
    I can't wait to go back to the windows Explorer, I don't have to.
    Let us, you should know that will not work with your updates until we start them. If I had known Albine would not work I wouldn't have updated.

    Thanks.
    

    There is a new version that contains fixes for version 17 (according to the changelog): https://addons.mozilla.org/en-US/firefox/addon/privacysuite/versions/

    Please do not use outdated versions of firefox as they will contain many known security vulnerabilities, which will put the integrity of your system and your personal data on this subject at risk!

  • have the latest version of Firefox and I like the new tab feature will not work with my version 13.0. 1

    I recently upgraded to Firefox 13.0.1 and received the email touting the new thumbnail feature of the tab, the only problem is when I try to download the new feature, I get an error message indicating that my version of Firefox is outdated and the thumbnail of the tab will not work with her and to upgrade.

    Each as indicated, but it only opened a new tab with no thumbnails again.
    So I'm puzzled as to why it does not work and my Firefox does not show Windows or Firefox 13 in the right sidebar below.

  • Qosmio G50 overheats while working with lid closed?

    If I work with the lid closed (i.e. turn on with the remote control) so that I just use two external monitors, the G50 will overheat or like other portable dockable, it's a very happy job like that?

    Does anyone know?

    Thanks in advance.

    Hello

    Why the Qosmio overheating?
    I want to say I put t see a link between overeating and closed or open lid

    The cooling fans are placed at the bottom of the unit and the air clear on the portable side so it s is not serious if the lid has been closed or not

    The laptop should not overheat

    Good bye

  • I680 can not get the Iden phone book software to work with the help of I680?

    I have 30 of I680 and I can't the iden phone book software on my PC to connect phones to manage contacts. Does anyone know how to manage contacts using a PC to the I680? Any help would be appreciated.

    DonK

    Sorry to all the world, iDEN Phonebook Manager on the site of the BIKE has just been upgraded ".» 07"version last Friday who will work with i680. If you '. " 05"or a version older than what was posted last weekend 2/21, please uninstall your current version of iPM and download our regular liaison:

    http://idenphones.Motorola.com/iDEN/support/software/organizer_upd.jsp

    If you had uninstalled the drivers or load for the first time;

    Unplug i680 of computer, first install the iDEN Phonebook Manager R01.03.07 before you connect the handset to the PC.

    Restart the computer

    Please make sure that you set the USB connection of your i680 to "Modem Data"

    Set the combined, allow your Windows computer in order to install Modem iDEN, P2k drivers drivers modem using normal Windows Install Wizard (it should find the appropriate SW if you select to install automatically).

    Continue to use MPI that you normally use.

  • The 1-754-399-11 antenna works with CMT-CX4iP for the FM reception

    Is antenna 1-754-399-11 work with this drive - CMT-CX4iP?

    Hi John,.

    Welcome to the community of Sony!

    This antenna is not compatible with the CMT-CX4IP. Manufacturer of FM lead antenna compatible is 1-754-459-41. This antenna is available for purchase online on our site of parts and accessories.

    https://servicesales.sel.Sony.com/Ecom/accessories/Web/ShoppingCart.do?operation=addToCart&Itemid=73...

    Please indicate the only accepted such solution if you find the information useful.

    Thank you for your message.

Maybe you are looking for

  • Portege R600 - keyboard replacement

    Hello I need a replacement keyboard for my Portege R600. I need a US international keyboard. Can you please tell me where I can buy that? Google me given links for R500 but not for R600. Also the part number would be helpful, for R500 is P000488460,

  • Not of his outgoing on Samsung Note with 4.4.2 Android

    So, I saw that the "community" here is a joke with ppl get very few answers. Care to prove me wrong?If the title says it all, but here's a little more:I scream that I can hear others that they cannot hear me.I get a call I can hear others that they c

  • CNC - text and background color changes when you use the inc/dec

    Hello I develop an application that has a sign with a black background and green or red numbers. I have added digital controls and set the text color and text background color attributes accordinglingy. I put the control digital hot or validate comma

  • Windows XP has online a glossary or a dictionary?

    WINDOWS XP HAS A LEXICON OR DICTIONARY = FOR WE CAN UNDERSTAND ALL THESE ABBREVIATIONS AND KNOW WHAT THEY MEAN? Thank you RFRANCO

  • MSVCP100.dll is missing error on Windows 8

    Original title: SYSTEM ERROR I just installed Windows 8 and I am getting an error, (I believe that Reader_sl.exe), it says: "could not start program because MSVCP100.dll file is missing" is - anyone know how to find and install this file. GeneY