calling non-static method in static method in Java

me123 picture me123 · Jan 11, 2010 · Viewed 401.7k times · Source

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).

Answer

danben picture danben · Jan 11, 2010

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.