views:

823

answers:

6

Hi,

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

Thanks for any help.

+7  A: 

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.

danben
+4  A: 

You could create an instance of the class you want to call the method on, e.g.

new Foo().nonStaticMethod();
Fabian Steeg
You're calling a static method on an instance of Foo?
danben
I believe this is possible, but best avoided. Perhaps the responder meant .instanceMethod() ...
Hamish Grubijan
Thanks for pointing out my typo, fixed that.
Fabian Steeg
+1  A: 

There are two ways:

  1. Call the non-static method from an instance within the static method. See fabien's answer for an oneliner sample... although I would strongly recommend against it. With his example he creates an instance of the class and only uses it for one method, only to have it dispose of it later. I don't recommend it because it treats an instance like a static function.
  2. Change the static method to a non-static.
monksy
+1  A: 

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.

Drew Wills
+4  A: 

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 class String without an instance:

 String.startsWith("Hello");

What you need is to have an instance and then invoke the non-static method:

 String greeting = new String("Hello World");
 greeting.startsWith("Hello"); // returns true 

So you need to create and instance to invoke it.

OscarRyz
+1  A: 

It sounds like the method really should be static (i.e. it doesn't access any data members and it doesn't need an instance to be invoked on). Since you used the term "static class", I understand that the whole class is probably dedicated to utility-like methods that could be static.

However, Java doesn't allow the implementation of an interface-defined method to be static. So when you (naturally) try to make the method static, you get the "cannot-hide-the-instance-method" error. (The Java Language Specification mentions this in section 9.4: "Note that a method declared in an interface must not be declared static, or a compile-time error occurs, because static methods cannot be abstract.")

So as long as the method is present in xInterface, and your class implements xInterface, you won't be able to make the method static.

If you can't change the interface (or don't want to), there are several things you can do:

  • Make the class a singleton: make the constructor private, and have a static data member in the class to hold the only existing instance. This way you'll be invoking the method on an instance, but at least you won't be creating new instances each time you need to call the method.
  • Implement 2 methods in your class: an instance method (as defined in xInterface), and a static method. The instance method will consist of a single line that delegates to the static method.
Eli Acherkan