我可以调用的静态成员的实例方法从静态范围内? [英] can I call instance method of a static member from within static context?

查看:206
本文介绍了我可以调用的静态成员的实例方法从静态范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

private static AppWidgetService mInstance = null;

public static void startRefresh() {

        AppWidgetProvider.setRefreshingState(mInstance
                .getApplicationContext());
        AppWidgetManager.refreshHandler(mInstance.getApplicationContext());
    }

这有时会失败在运行时,有时通过了。

It sometimes fails in runtime, and sometimes passes.

对于obviuse异常:

for the obviuse exception:

不能调用从静态上下文的非静态方法

我很困惑, mInstance 是静态的,

所以它的实例方法可以从静态上下文中调用。不是吗?

so its instance methods could be called from a static context. no?

那么怎么来的,如果有时会失败?

then how come if sometimes fails?

推荐答案

从静态函数,你只能调用静态函数或使用静态变量。该链接是在运行时完成。所以,虽然你的编辑将被罚款,但在运行时调用时将失败。请尝试将功能非静态的,如果你想打这通电话。

From a static function, you can call only static function or use static variable. The linking is done at run time. So, though your compilation will be fine but at run time it will fail when call is made. Try making your function non static if you want to make that call.

调用从静态方法非静态方法的唯一办法是有一个包含非静态方法的类的实例。根据定义,非静态方法是一个被称为一些类的实例,而静态方法属于类本身。

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.

这篇关于我可以调用的静态成员的实例方法从静态范围内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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