vendredi 3 juin 2016

issue in accessing the value of final variable after changing it value by reflection?

 class FinalConcept

{ private final int number = 22;

   public static void main(String args[])
    { 
          try{
        FinalConcept obj  = new FinalConcept();      
        System.out.println("value of the x variable "+obj.number);


       Field obj1 = obj.getClass().getDeclaredField("number");
        obj1.setAccessible(true);
         obj1.setInt(obj,45); 

// if i try to access the variable by field function then i get changed value

System.out.println("value of the x variable "+obj1.get(obj));//45

// but if i try to access by the name of variable i got the same value

System.out.println("value of x varialbe "+obj.x);//22

why this is happen ?





Aucun commentaire:

Enregistrer un commentaire