This question already has an answer here:
I'm getting an error when I try to call a non-static method in a static class.
Cannot make a static reference to the non-static method methodName() from the type playback
I can't make the method static as this gives me an error too.
This static method cannot hide the instance method from xInterface
Is there any way to get round calling an non-static method in another static method? (The two methods are in seperate packages and seperate classes).
The only way to call a non-static method from a static method is to have an instance of the class containing the non-static method. By definition, a non-static method is one that is called ON an instance of some class, whereas a static method belongs to the class itself.
Constructor is a special method which in theory is the "only" non-static method called by any static method. else its not allowed.
There are two ways:
You need an instance of the class containing the non static method.
Is like when you try to invoke the non-static method
startsWith
of classString
without an instance:What you need is to have an instance and then invoke the non-static method:
So you need to create and instance to invoke it.
You can't get around this restriction directly, no. But there may be some reasonable things you can do in your particular case.
For example, you could just "new up" an instance of your class in the static method, then call the non-static method.
But you might get even better suggestions if you post your class(es) -- or a slimmed-down version of them.
The only way to call a non-static method from a static method is to have an instance of the class containing the non-static method.