Java中静态方法中调用非静态方法
当我尝试在静态类中调用非静态方法时出现错误。
无法从类型播放中静态引用非静态方法 methodName()
我无法将该方法设为静态,因为这也会给我带来错误。
此静态方法无法从 xInterface 隐藏实例方法
有没有办法绕过在另一个静态方法中调用非静态方法? (这两种方法位于单独的包和单独的类中)。
I'm getting an error when I try to call a non-static method in a static class.
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.
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
从静态方法调用非静态方法的唯一方法是拥有包含非静态方法的类的实例。根据定义,非静态方法是在某个类的实例上调用的方法,而静态方法属于该类本身。
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.
您可以创建要调用该方法的类的实例,例如
You could create an instance of the class you want to call the method on, e.g.
首先创建一个类实例并使用该实例调用非静态方法。
例如,
Firstly create a class Instance and call the non-static method using that instance.
e.g,
上面的代码没有执行,因为静态方法必须具有该类引用。
这肯定会被执行的。因为在这里我们通过使用该类的引用来创建除了“sm”之外什么都没有的引用
但是 (
StaticMethod=new Static method()
) 我们正在调用方法一 (sm.methodOne()
)。我希望这会有所帮助。
the above code not executed because static method must have that class reference.
This will be definitely get executed. Because here we are creating reference which nothing but "sm" by using that reference of that class which is nothing
but (
StaticMethod=new Static method()
) we are calling method one (sm.methodOne()
).I hope this will be helpful.
您需要包含非静态方法的类的实例。
就像当您尝试在没有实例的情况下调用类
String
的非静态方法startsWith
时:您需要的是拥有一个实例,然后调用非静态方法:
所以你需要创建实例来调用它。
You need an instance of the class containing the non static method.
Is like when you try to invoke the non-static method
startsWith
of classString
without an instance:What you need is to have an instance and then invoke the non-static method:
So you need to create and instance to invoke it.
听起来该方法实际上应该是静态的(即它不访问任何数据成员,并且不需要调用实例)。由于您使用了术语“静态类”,我知道整个类可能专用于可能是静态的类似实用程序的方法。
但是,Java 不允许接口定义方法的实现是静态的。因此,当您(自然地)尝试使该方法成为静态时,您会收到“无法隐藏实例方法”错误。 (Java 语言规范在 第 9.4 节中提到了这一点: “请注意,接口中声明的方法一定不能声明为静态,否则会发生编译时错误,因为静态方法不能是抽象的。”)
因此只要该方法存在于
xInterface
,并且您的类实现了xInterface
,您将无法将该方法设为静态。如果您无法更改接口(或不想更改),您可以执行以下操作:
It sounds like the method really should be static (i.e. it doesn't access any data members and it doesn't need an instance to be invoked on). Since you used the term "static class", I understand that the whole class is probably dedicated to utility-like methods that could be static.
However, Java doesn't allow the implementation of an interface-defined method to be static. So when you (naturally) try to make the method static, you get the "cannot-hide-the-instance-method" error. (The Java Language Specification mentions this in section 9.4: "Note that a method declared in an interface must not be declared static, or a compile-time error occurs, because static methods cannot be abstract.")
So as long as the method is present in
xInterface
, and your class implementsxInterface
, you won't be able to make the method static.If you can't change the interface (or don't want to), there are several things you can do:
xInterface
), and a static method. The instance method will consist of a single line that delegates to the static method.从静态方法调用非静态方法的唯一方法是拥有包含非静态方法的类的实例。
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.
有两种方法:
There are two ways:
你不能直接绕过这个限制,不。但根据您的具体情况,您可能可以采取一些合理的措施。
例如,您可以在静态方法中“新建”类的实例,然后调用非静态方法。
但是,如果您发布您的课程或它们的精简版本,您可能会得到更好的建议。
You can't get around this restriction directly, no. But there may be some reasonable things you can do in your particular case.
For example, you could just "new up" an instance of your class in the static method, then call the non-static method.
But you might get even better suggestions if you post your class(es) -- or a slimmed-down version of them.
在静态方法中使用非静态方法/字段的最简单方法,反之亦然是...
(要实现这一点,必须至少有一个此类的实例)
这种情况在 Android 应用程序开发中非常常见,例如:- 一项活动至少有一个实例。
注意:- 这不能用作像这样的构建器方法......
The easiest way to use a non-static method/field within a a static method or vice versa is...
(To work this there must be at least one instance of this class)
This type of situation is very common in android app development eg:- An Activity has at-least one instance.
Note:- This cannot be used as a builder method like this one.....
我使用一个接口并创建它的匿名实例,如下所示:
AppEntryPoint.java
Main.java
不像创建该类的实例并调用其自己的方法那么优雅,但本质上完成了相同的事情。只是另一种方式来做到这一点。
I use an interface and create an anonymous instance of it like so:
AppEntryPoint.java
Main.java
Not as elegant as creating an instance of that class and calling its own method, but accomplishes the same thing, essentially. Just another way to do it.
静态方法中不能调用非静态方法。其背后的逻辑是我们不创建一个对象来实例化静态方法,但我们必须创建一个对象来实例化非静态方法。因此,非静态方法不会在静态方法内部实例化时获取对象,从而使其无法被实例化。
It is not possible to call non-static method within static method. The logic behind it is we do not create an object to instantiate static method, but we must create an object to instantiate non-static method. So non-static method will not get object for its instantiation inside static method, thus making it incapable for being instantiated.
构造函数是一种特殊的方法,理论上它是任何静态方法调用的“唯一”非静态方法。否则是不允许的。
Constructor is a special method which in theory is the "only" non-static method called by any static method. else its not allowed.
您可以使用以下方法在静态方法中调用非静态方法:
类名.class.method()
You can call a non static method within a static one using:
Classname.class.method()