在Java的静态方法中调用非静态方法
当我试图在静态类中调用非静态方法时出错 无法从播放类型对非静态方法methodName()进行静态引用 我不能使该方法静态,因为这也给了我一个错误 此静态方法无法对xInterface隐藏实例方法在Java的静态方法中调用非静态方法,java,static,non-static,Java,Static,Non Static,当我试图在静态类中调用非静态方法时出错 无法从播放类型对非静态方法methodName()进行静态引用 我不能使该方法静态,因为这也给了我一个错误 此静态方法无法对xInterface隐藏实例方法 有没有办法绕过在另一个静态方法中调用非静态方法?(这两个方法分别位于不同的包和不同的类中)。从静态方法调用非静态方法的唯一方法是使用包含非静态方法的类的实例。根据定义,非静态方法是在某个类的实例上调用的方法,而静态方法属于该类本身。您可以创建要调用该方法的类的实例,例如 new Foo().nonSt
有没有办法绕过在另一个静态方法中调用非静态方法?(这两个方法分别位于不同的包和不同的类中)。从静态方法调用非静态方法的唯一方法是使用包含非静态方法的类的实例。根据定义,非静态方法是在某个类的实例上调用的方法,而静态方法属于该类本身。您可以创建要调用该方法的类的实例,例如
new Foo().nonStaticMethod();
有两种方法:
你不能直接绕过这个限制,不。但是在你的特殊情况下,你可以做一些合理的事情 例如,您可以在静态方法中“新建”类的实例,然后调用非静态方法
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*/
}
}
但是如果你发布你的类或者它们的精简版本,你可能会得到更好的建议。你需要一个包含非静态方法的类实例 类似于在没有实例的情况下尝试调用类
String
的非静态方法startsWith
:
String.startsWith("Hello");
您需要的是拥有一个实例,然后调用非静态方法:
String greeting = new String("Hello World");
greeting.startsWith("Hello"); // returns true
因此,您需要创建一个实例来调用它 听起来该方法应该是静态的(即,它不访问任何数据成员,也不需要调用实例)。因为您使用了术语“静态类”,所以我理解整个类可能专用于可能是静态的实用程序类方法 然而,Java不允许接口定义方法的实现是静态的。因此,当您(自然地)尝试将该方法设置为静态时,会出现“无法隐藏实例方法”错误。(Java语言规范在“注意,接口中声明的方法不能声明为静态的,否则会发生编译时错误,因为静态方法不能是抽象的。”) 因此,只要该方法存在于
xInterface
中,并且您的类实现了xInterface
,就不能使该方法成为静态的
如果您无法更改接口(或不想更改),可以做以下几件事:
- 将类设为单例:将构造函数设为私有,并在类中有一个静态数据成员来保存唯一的现有实例。这样,您将在实例上调用该方法,但至少不会在每次需要调用该方法时都创建新实例
- 在类中实现两个方法:实例方法(定义见
)和静态方法。实例方法将由一行组成,该行委托给静态方法xInterface
构造函数是一种特殊的方法,理论上它是任何静态方法调用的“唯一”非静态方法。否则是不允许的。在静态方法中使用非静态方法/字段或在静态方法中使用非静态方法/字段的最简单方法是 (若要执行此操作,必须至少有一个此类实例) 这种情况在android应用程序开发中非常常见,例如:-一个活动至少有一个实例
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);
您可以使用以下方法在静态方法中调用非静态方法:
Classname.class.method()
从静态方法调用非静态方法的唯一方法是使用包含非静态方法的类的实例
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*/
}
}
我使用一个接口并创建一个匿名实例,如下所示: 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;
}
}
不像创建该类的实例并调用它自己的方法那样优雅,但本质上完成了相同的事情。只是另一种方法
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=newstaticmethod()
)我们正在调用方法一(sm.methodOne()
)
我希望这会有帮助。在静态方法中调用非静态方法是不可能的。其背后的逻辑是我们不创建一个对象来实例化静态方法,但我们必须创建一个对象来实例化非静态方法。所以,非静态方法不会在静态方法内获取实例化对象,从而使其无法实例化 在类和方法之间添加
.class
确实有效,但您不能。这只是幻想,除非您要调用的方法是在java.lang.Class
中定义的。问题不是关于构造函数。如果您有一个实例来调用非statc方法,这是可能的。例如,它可以作为参数提供,也可以作为类的静态数据成员提供。实际上,据我所知,用实例调用非静态方法超出了这个问题的范围。@EJP出于安全目的,在d的情况下,我对我的项目就是这样做的