134

当我尝试在静态类中调用非静态方法时出现错误。

无法从类型回放中对非静态方法 methodName() 进行静态引用

我不能使方法静态,因为这也给了我一个错误。

此静态方法无法从 xInterface 隐藏实例方法

有没有办法在另一个静态方法中调用非静态方法?(这两种方法在单独的包和单独的类中)。

4

14 回答 14

163

从静态方法调用非静态方法的唯一方法是拥有一个包含非静态方法的类的实例。根据定义,非静态方法是在某个类的实例上调用的方法,而静态方法属于类本身。

于 2010-01-11T15:40:15.847 回答
102

您可以创建要调用该方法的类的实例,例如

new Foo().nonStaticMethod();
于 2010-01-11T15:40:53.477 回答
66

首先创建一个类 Instance 并使用该实例调用非静态方法。例如,

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);
    }
}
于 2012-06-12T08:14:19.170 回答
14
public class StaticMethod{

    public static void main(String []args)throws Exception{
        methodOne();
    }

    public int methodOne(){
        System.out.println("we are in first methodOne");
        return 1;
    }
}

上述代码未执行,因为静态方法必须具有该类引用。

public class StaticMethod{
    public static void main(String []args)throws Exception{

        StaticMethod sm=new StaticMethod();
        sm.methodOne();
    }

    public int methodOne(){
        System.out.println("we are in first methodOne");
        return 1;
    }
}

这肯定会被执行。因为在这里我们通过使用该类的引用创建引用,该引用只不过是“sm”,StaticMethod=new Static method()我们调用方法一(sm.methodOne())。

我希望这会有所帮助。

于 2015-11-25T06:02:24.897 回答
7

您需要一个包含非静态方法的类的实例。

startsWith就像您尝试在没有实例的情况下调用类的非静态方法时一样String

 String.startsWith("Hello");

您需要的是有一个实例,然后调用非静态方法:

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

所以你需要创建和实例来调用它。

于 2010-01-11T15:45:56.833 回答
7

听起来该方法确实应该是静态的(即它不访问任何数据成员,也不需要调用实例)。由于您使用了“静态类”一词,我知道整个类可能专用于可能是静态的类似实用程序的方法。

但是,Java 不允许接口定义方法的实现是静态的。因此,当您(自然地)尝试将方法设为静态时,您会收到“无法隐藏实例方法”错误。(Java 语言规范在第 9.4 节中提到了这一点:“请注意,在接口中声明的方法不能声明为静态,否则会发生编译时错误,因为静态方法不能是抽象的。”

因此,只要该方法存在于 中xInterface,并且您的类实现了xInterface,您就无法使该方法成为静态的。

如果你不能改变界面(或者不想改变),你可以做几件事:

  • 将类设为单例:将构造函数设为私有,并在类中有一个静态数据成员来保存唯一现有的实例。这样,您将在实例上调用该方法,但至少您不会在每次需要调用该方法时都创建新实例。
  • 在您的类中实现 2 个方法:一个实例方法(如 中所定义xInterface)和一个静态方法。实例方法将由代表静态方法的单行组成。
于 2010-01-11T16:09:09.580 回答
4

从静态方法调用非静态方法的唯一方法是拥有一个包含非静态方法的类的实例。

class A
{
    void method()
    {
    }
}
class Demo
{
    static void method2()
    {
        A a=new A();

        a.method();
    }
    /*
    void method3()
    {
        A a=new A();
        a.method();
    }
    */

    public static void main(String args[])
    {
        A a=new A();
        /*an instance of the class is created to access non-static method from a static method */
        a.method();

        method2();

        /*method3();it will show error non-static method can not be  accessed from a static method*/
    }
}
于 2015-01-27T18:08:10.410 回答
1

有两种方法:

  1. 从静态方法中的实例调用非静态方法。请参阅 fabien 对 oneliner 样本的回答……尽管我强烈建议不要这样做。在他的示例中,他创建了该类的一个实例,并且仅将其用于一种方法,只是稍后将其处理掉。我不推荐它,因为它将实例视为静态函数。
  2. 将静态方法更改为非静态方法。
于 2010-01-11T15:40:53.913 回答
1

你不能直接绕过这个限制,不。但是在您的特定情况下,您可以做一些合理的事情。

例如,您可以在静态方法中“新建”类的一个实例,然后调用非静态方法。

但是,如果您发布您的课程或精简版的课程,您可能会得到更好的建议。

于 2010-01-11T15:42:41.100 回答
1

在静态方法中使用非静态方法/字段的最简单方法是……

(要做到这一点,必须至少有一个此类的实例)

这种情况在 android 应用程序开发中非常常见,例如:- 一个 Activity 至少有一个实例。

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();
      }
   }
}

注意:- 这不能用作像这样的构建器方法.....

String.valueOf(100);
于 2014-09-01T14:34:23.820 回答
1

我使用一个接口并创建一个匿名实例,如下所示:

AppEntryPoint.java

public interface AppEntryPoint
{
    public void entryMethod();
}

主.java

public class Main
{
    public static AppEntryPoint entryPoint;

    public static void main(String[] args)
    {
        entryPoint = new AppEntryPoint()
        {

            //You now have an environment to run your app from

            @Override
            public void entryMethod()
            {
                //Do something...
                System.out.println("Hello World!");
            }
        }

        entryPoint.entryMethod();
    }

    public static AppEntryPoint getApplicationEntryPoint()
    {
        return entryPoint;
    }
}

不像创建该类的实例并调用它自己的方法那样优雅,但本质上完成了同样的事情。只是另一种方法。

于 2015-10-16T21:16:50.490 回答
1

不能在静态方法中调用非静态方法。其背后的逻辑是我们不创建对象来实例化静态方法,而是必须创建对象来实例化非静态方法。所以非静态方法在静态方法内部实例化时不会获取对象,因此无法实例化。

于 2017-05-02T06:43:42.660 回答
0

构造函数是一种特殊的方法,理论上它是任何静态方法调用的“唯一”非静态方法。否则它是不允许的。

于 2013-10-09T23:11:13.583 回答
0

您可以使用以下方法在静态方法中调用非静态方法: Classname.class.method()

于 2014-12-30T11:53:32.193 回答