最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】VSCode中java代码警告:The static method from the type should be accessed in a static way

Java crifan 2785浏览 0评论
折腾:
【未解决】VSCode中调试java报错:Build failed do you want to continue
期间,看到代码警告:
The static method address(int, int) from the type Util should be accessed in a static way
public class ParamePreset {
  private Util util = new Util();

  builder.append(util.address(infoElement[0], infoElement[1]));
感觉应该是:把new出来的util,直接用Util就对了。
The static method address(int, int) from the type Util should be accessed in a static way
The static method from the type should be accessed in a static way
java – Why should the static field be accessed in a static way? – Stack Overflow
果然是这意思。
java – Warning: static method should be accessed in a static way – Stack Overflow
不过试试快速修复:
Change access to static using ‘Util’ (declaring type)
明显,不推荐用
Remove static modifier of address()
然后:
变成了:
builder.append(Util.address(infoElement[0], infoElement[1]));
此处也顺带去看看Util类中的函数:
果然很多都是static函数
所以别处调用函数,应该是直接用类名即可,而不是类的实例。
如此,继续改动其他代码,即可。
【总结】
此处,java代码中,和其他面向对象编程,比如Python语言,是类似的。
对于static的函数,应该用类,而非实例去调用。
即:
YourClass.someStaticFunction()
而不是:
yourClass = new YourClass()
yourClass.someStaticFunction()
如果你的YourClass中的someStaticFunction是static的话:
class YourClass{
  public static someStaticFunction(){
  }
}

转载请注明:在路上 » 【已解决】VSCode中java代码警告:The static method from the type should be accessed in a static way

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.198 seconds, using 22.09MB memory