Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to know if reflection in android can affect the outside class (class A)
lets say i have class A
which have a method setData:

Java
public void setData(int id){
    editText1.setText(id+"");
}


and class B ;
I send A.class as parameter to class B represent by c.

Java
Object obj = c.newInstance();
Method method1 = c.getMethod("setData", new Class[]{Integer.TYPE});
method1.setAccessible(true);
method1.invoke(obj, 3)

this code is working in the inside class(class B) but outside in the original class A like I didn't change anything.

thanks in advance.

What I have tried:

I tried to trace the data send i.e id after invoke the method in class B and print it in both classes it changed in Class B only class A has no effect
Posted
Comments
Richard MacCutchan 26-Jun-16 8:14am    
Maybe the call to newInstance is the problem. I think you should be calling the method on the existing object that holds the reference to editText1.
[no name] 26-Jun-16 8:20am    
I need to create the object in Class B it is some kind of approach I have to use ,in class A there will be no object just send the class and in class B create the object and access the methods from it , I just wanna know if it is not possible so I can change the approach.
thanks for your response.
Richard MacCutchan 26-Jun-16 12:20pm    
In future please use the Reply button above a message to respond to it.

I don't understand this question, and I still think that creating a new object is not the solution to your problem.
Sergey Alexandrovich Kryukov 26-Jun-16 9:38am    
This is not a question, sorry.

It can gather information on the class, metadata and can be used to invoke some method/property, in particular, to instantiate a class.
What exactly do you want to achieve?

—SA
[no name] 26-Jun-16 10:15am    
Simply I want ,if I have a value id and setId(int id) & getId() methods in class A I want to call setId(3)id in class B using method.invoke, and call getId() from class A to return 3.
sorry if it's not clear English is not my native language.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900