【发布时间】:2011-01-03 19:25:54
【问题描述】:
当我尝试在静态类中调用非静态方法时遇到错误。
无法从类型回放中对非静态方法methodName()进行静态引用
我不能将方法设为静态,因为这也会给我一个错误。
此静态方法无法从 xInterface 隐藏实例方法
有没有办法绕过在另一个静态方法中调用非静态方法? (这两种方法在单独的包和单独的类中)。
【问题讨论】:
标签: java static non-static
当我尝试在静态类中调用非静态方法时遇到错误。
无法从类型回放中对非静态方法methodName()进行静态引用
我不能将方法设为静态,因为这也会给我一个错误。
此静态方法无法从 xInterface 隐藏实例方法
有没有办法绕过在另一个静态方法中调用非静态方法? (这两种方法在单独的包和单独的类中)。
【问题讨论】:
标签: java static non-static
从静态方法调用非静态方法的唯一方法是拥有一个包含非静态方法的类的实例。根据定义,非静态方法是在某个类的实例上调用的方法,而静态方法属于该类本身。
【讨论】:
您可以创建要调用该方法的类的实例,例如
new Foo().nonStaticMethod();
【讨论】:
首先创建一个类实例并使用该实例调用非静态方法。 例如,
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;
}
}
这肯定会被执行。因为在这里我们通过使用那个什么都不是的类的引用来创建只有“sm”的引用
但是 (StaticMethod=new Static method()) 我们正在调用方法一 (sm.methodOne())。
我希望这会有所帮助。
【讨论】:
您需要一个包含非静态方法的类的实例。
就像当您尝试在没有实例的情况下调用类String 的非静态方法startsWith 时:
String.startsWith("Hello");
你需要有一个实例,然后调用非静态方法:
String greeting = new String("Hello World");
greeting.startsWith("Hello"); // returns true
所以你需要创建和实例来调用它。
【讨论】:
听起来该方法真的应该是静态的(即它不访问任何数据成员,也不需要调用实例)。由于您使用了术语“静态类”,我知道整个类可能专用于可能是静态的类似实用程序的方法。
但是,Java 不允许接口定义方法的实现是静态的。因此,当您(自然地)尝试使方法静态时,您会收到“无法隐藏实例方法”错误。 (Java 语言规范在section 9.4 中提到了这一点:“请注意,接口中声明的方法不能声明为静态,否则会发生编译时错误,因为静态方法不能是抽象的。” )
只要该方法存在于xInterface 中,并且您的类实现了xInterface,您就无法将该方法设为静态。
如果你不能改变界面(或者不想改变),你可以做几件事:
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*/
}
}
【讨论】:
有两种方式:
【讨论】:
你不能直接绕过这个限制,不。但在您的特定情况下,您可能会做一些合理的事情。
例如,您可以在静态方法中“新建”类的实例,然后调用非静态方法。
但是,如果您发布您的课程或精简版的课程,您可能会得到更好的建议。
【讨论】:
在静态方法中使用非静态方法/字段的最简单方法是……
(要做到这一点,必须至少有一个此类的实例)
这种情况在 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);
【讨论】:
我使用一个接口并像这样创建它的一个匿名实例:
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;
}
}
不像创建该类的实例并调用它自己的方法那样优雅,但本质上完成了同样的事情。只是另一种方法。
【讨论】:
不能在静态方法中调用非静态方法。其背后的逻辑是我们不创建对象来实例化静态方法,而是必须创建对象来实例化非静态方法。所以非静态方法在静态方法内部实例化时不会获取对象,因此无法实例化。
【讨论】:
构造函数是一种特殊方法,理论上它是任何静态方法调用的“唯一”非静态方法。否则不允许。
【讨论】:
您可以使用以下方法在静态方法中调用非静态方法:
Classname.class.method()
【讨论】:
.class 确实有效,谢谢
java.lang.Class。