在Java中调用静态方法中的非静态方法

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

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

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

这个静态方法不能从xInterface隐藏实例方法

有没有什么办法来调用另一个静态方法中的非静态方法? (这两种方法分别是单独的包和单独的类)。

从静态方法调用非静态方法的唯一方法是让包含非静态方法的类的实例。 根据定义,非静态方法是一个被称为ON的某个类的实例,而一个静态方法属于该类本身。

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

new Foo().nonStaticMethod(); 

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

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

这将是肯定得到执行。 因为在这里,我们通过使用该类的引用来创build除“(”)之外什么也不是“sm”的引用,我们正在调用方法一( sm.methodOne() ),该引用只是( StaticMethod=new Static method() sm.methodOne() )。

我希望这会有所帮助。

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

就像当你试图调用类的String的非静态方法startsWith没有一个实例:

  String.startsWith("Hello"); 

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

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

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

这听起来像方法真的应该是静态的(即它不访问任何数据成员,它不需要调用一个实例)。 既然你使用了“静态类”这个术语,我知道整个类可能是专门用于类似于工具的方法,可能是静态的。

但是,Java不允许实现接口定义的方法是静态的。 所以当你(自然地)试图使这个方法变成静态时,你会得到“不能隐藏实例方法”的错误。 (Java语言规范在第9.4节中提到: “请注意,在接口中声明的方法不能声明为静态的,否则会发生编译时错误,因为静态方法不能是抽象的。

所以只要方法存在于xInterface ,并且你的类实现了xInterface ,你就不能使这个方法成为静态的。

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

  • 使类成为单例:使构造函数为私有的,并在类中有一个静态数据成员来保存唯一的现有实例。 这样你就可以调用一个实例的方法,但是至less每次你需要调用这个方法时你都不会创build新的实例。
  • 在你的类中实现2个方法:一个实例方法(在xInterface定义)和一个静态方法。 实例方法将由一个委托给静态方法的单个行组成。

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

 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*/ } } 

有两种方法:

  1. 从静态方法中的实例调用非静态方法。 看到fabien的答案为一个在线样本…虽然我会强烈build议反对它。 以他的例子来说,他创build了一个类的实例,只用它来处理一个方法,只是稍后处理它。 我不推荐它,因为它像一个静态函数一样处理一个实例。
  2. 将静态方法更改为非静态方法。

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

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

但是如果你发布了你的课程,或者你的课程减less了,你可能会得到更好的build议。

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

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

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

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

注意: – 这不能用作像这样的build造者方法…..

 String.valueOf(100); 

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

AppEntryPoint.java

 public interface AppEntryPoint { public void entryMethod(); } 

Main.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; } } 

不如创build该类的实例并调用其自己的方法,但实质上完成相同的事情。 只是另一种方式来做到这一点。

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

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

静态方法中不可能调用非静态方法。 它背后的逻辑是我们不创build一个对象来实例化静态方法,但是我们必须创build一个对象来实例化非静态方法。 所以非静态方法在静态方法中不会得到实例化的对象,从而使其不能被实例化。