lundi 20 février 2017

Change java method inner variables value from other methods in the same class

Here's a Interview question.

Qusetion:

public class test {
public static void main(String[] args) {

    int a= 10;
    int b=10;
    method(a,b);
    System.out.println(a);
    System.out.println(b);
}

public static void method(int a,int b){
    //**Under the premise of not changing the original question, how to write this function in the main function to output a=100,b=200?**

}
}

Answers : enter image description here

So, my questions are :

1.Why [a.getClass().getDeclaredField("value")] can get variabe "a" from method main ? I checked each item of [a.getClass().getDeclaredFields] returned array in debug mode,but did not find any regularities. Are there any refers about every item meaning of [a.getClass().getDeclaredFields] returned array.

2.I know that methods' inner variables save in stack memory,and shared in the same thread . Can we change variable main's "a" value through Reflection or Using a new Java classloader ?





Aucun commentaire:

Enregistrer un commentaire