calling non-static method in static method in Java

2018-12-31 03:02发布

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

14条回答
与君花间醉酒
2楼-- · 2018-12-31 03:16

You can call a non static method within a static one using: Classname.class.method()

查看更多
骚的不知所云
3楼-- · 2018-12-31 03:18

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

new Foo().nonStaticMethod();
查看更多
像晚风撩人
4楼-- · 2018-12-31 03:18

Firstly create a class Instance and call the non-static method using that instance. e.g,

class demo {

    public static void main(String args[]) {
        demo d = new demo();
        d.add(10,20);     // to call the non-static method
    }

    public void add(int x ,int y) {
        int a = x;
        int b = y;
        int c = a + b;
        System.out.println("addition" + c);
    }
}
查看更多
余生请多指教
5楼-- · 2018-12-31 03:23

It is not possible to call non-static method within static method. The logic behind it is we do not create an object to instantiate static method, but we must create an object to instantiate non-static method. So non-static method will not get object for its instantiation inside static method, thus making it incapable for being instantiated.

查看更多
时光乱了年华
6楼-- · 2018-12-31 03:26

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.
查看更多
心情的温度
7楼-- · 2018-12-31 03:28

The easiest way to use a non-static method/field within a a static method or vice versa is...

(To work this there must be at least one instance of this class)

This type of situation is very common in android app development eg:- An Activity has at-least one instance.

public class ParentClass{

private static ParentClass mParentInstance = null;

ParentClass(){
  mParentInstance = ParentClass.this;           
}


void instanceMethod1(){
}


static void staticMethod1(){        
    mParentInstance.instanceMethod1();
}


public static class InnerClass{
      void  innerClassMethod1(){
          mParentInstance.staticMethod1();
          mParentInstance.instanceMethod1();
      }
   }
}

Note:- This cannot be used as a builder method like this one.....

String.valueOf(100);
查看更多
登录 后发表回答