I have a problem calling a static method from a class. Let me explain myself.
I have an interface Foo and his implementation FooImpl which defines the method getFoo(); :
public interface Foo {
...
public A getFoo();
....
}
public class FooImpl implements Foo {
public A getFoo(){
....
return new A();
}
}
This interface and his implementation is provided and I cannot modify them. In my program, I define a class called Bar in which the method getFoo is always called:
public class Bar {
Foo foo
public void fooBar(){
......
foo.getFoo()
....
}
}
My problem is that I would to make a static call to the method fooBar of the class Bar but it is impossible since the method getFoo is not defined as static.
For instance, I would like to do something like this :
public class Bar2 {
public void execute(){
Bar.fooBar()
}
}
How can I achieve that ?
Thanks for your advice
[EDIT]
Sorry if I am not clear. The class Bar has a reference to Foo, so this is why it is possible to call getFoo in the class. And I can guarantee that Foo/FooImpl are properly initialized (not by me) and I just use the informations provided by this interface.
To call
Bar.fooBar()in that way, it would have to be static, period.You haven’t shown how
Bargets an instance ofFooorFooImplon which to callgetFoo()– could there be a staticFoovariable inBarfor example?Basically there isn’t enough information here, but sooner or later you’ll have to have an instance of
FooImplat least, and quite possibly an instance ofBar, depending on its requirements.