Java中静态方法调用非静态方法 [英] Calling Non-Static Method In Static Method In Java
问题描述
当我尝试在静态类中调用非静态方法时出现错误.
I'm getting an error when I try to call a non-static method in a static class.
不能从类型回放中静态引用非静态方法methodName()
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.
此静态方法无法对 xInterface 隐藏实例方法
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).
推荐答案
从静态方法调用非静态方法的唯一方法是拥有一个包含非静态方法的类的实例.根据定义,非静态方法是在某个类的实例上调用的方法,而静态方法属于该类本身.
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.
这篇关于Java中静态方法调用非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!