<font color=blue> IS IT POSSIBLE</font>
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sabu_vs
Posted On:   Tuesday, May 14, 2002 09:31 PM

Hi I have 2 classes.class A and class B. class A contains 2 variables, String x and String y. I want to change the value of this variables in class A using class b using reflection. ie --------------------------------- class A { String x,y System.out.println("before"+x+y); B= new B(this); //passing object to the class B System.out.println("after"+x+y); } class B { public B(Object o) { // checking the fields of this object using re   More>>

Hi



I have 2 classes.class A and class B.


class A contains 2 variables, String x and String y.


I want to change the value of this variables in class A using class b using reflection.


ie




---------------------------------
class A

{





String x,y


System.out.println("before"+x+y);

B= new B(this); //passing object to the class B


System.out.println("after"+x+y);

}









class B
{



public B(Object o)
{



// checking the fields of this object using reflection


// changing the values of the fields


}



Could you pls tell me is it possible
If yes How can i do this



Thanks


sabu



}

   <<Less

Re: <font color=blue> IS IT POSSIBLE</font>

Posted By:   Simon_Ablett  
Posted On:   Wednesday, May 15, 2002 01:44 AM

Your 'x' and 'y' variables seem to be local to your method in class 'A' and not members of that class. You cannot access these from 'B'. Make them member attributes of 'A'
if you want to get to them. Reflection is of no use to you here as it still won't give you access to the locally scoped attributes of a method.

As an aside, it's never a good idea to let an object directly have access to the attributes of another object - always use accessor and mutator methods.

Regards.
About | Sitemap | Contact