Monday, May 14, 2012

Access object created in one class into another

I have a primary class as below:



public class classB{

public classC getObject(String getstring){
return new classC(getstring);
}
}


The classC has a contructor:



public class classC{

String string;

public classC(String s){
this.string = s;
}

public methodC(int i){
<using the `string` variable here>
}
}


Now I've a classA which will be using the object created in classB(which is of course, an instance of classC).



public classA{
int a = 0.5;

<Get the object that was created in classB>.methodC(a);

}


This is needed as a variable is created on some actions from the user and stored in classB and this would be further used in classC's methods. Creating a new object will render my variable in classB set to null which isn't intended.



How can I achieve this?



Please help!





No comments:

Post a Comment