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?
Assume the
Brand
is a lightweight objects andRun
is heavyweight then creating a field with the container for the lightweight objects and hiding it is a good idea.But the
Brand
needs access the container it belongs to it could be done with the mapping but we are simply inject theRun
to theBrand
so it's better implement theRunable
or annotate it with JSR330. And accessing the container through theRun
in the normal way.If you want to get the object created in classB a static field should do the job
Then you can access the reference in A
Also please follow the language conventions and start your class names with capital letters.