不能让一个静态引用非静态字段...但我不是一个静态引用 [英] Cannot make a static reference to the non-static field... but I am not making a static reference

查看:93
本文介绍了不能让一个静态引用非静态字段...但我不是一个静态引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一样很困惑如何/为什么这可能是一个问题:

I am quite confused as how/why this could be a problem:

public static long someMethod
{
  long local_temp_var = PackageInfo.lastUpdateTime;    
}

给出错误信息

无法使静态参考非静态字段
  PackageInfo.lastUpdateTime

Cannot make a static reference to the non-static field PackageInfo.lastUpdateTime

错误messafe要求我试图使静态参考?我真的不相信我。是,它是在一个静止/类的方法,但该变量是本地的。

The error messafe claims I am trying to make a static reference? I don't really believe I am. Yes, it is in a static/class method, but the variable is a local one.

推荐答案

确定。问题是,在 PackageInfo 变量 lastUpdateTime 也不是一成不变的!但是,你在呼唤它,仿佛它是。

Ok. the problem is that the variable lastUpdateTime in PackageInfo IS NOT static! But you are calling it as if it was.

PackageInfo 的JavaDoc的

您必须创建 PackageInfo 又名...

You have to create a new instance of PackageInfo aka...

PackageInfo信息=新PackageInfo();

那么你可以做....

then you can do....

长local_temp_var = info.lastUpdateTime; //注意从上面

这篇关于不能让一个静态引用非静态字段...但我不是一个静态引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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