在Java中调用静态方法中的非静态方法 [英] calling non-static method in static method in Java

查看:570
本文介绍了在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).

推荐答案

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆