Warning: Static member accessed via instance reference

Warning:

Static member accessed via instance reference

Shows references to static methods and fields via class instance rather than a class itself.

 

翻译:

通过引用实例来访问静态成员

通过类的实例来显示静态方法和变量的引用,而不是通过类本身

 

分析:

可能是考虑到实例会被回收

 

解决方案:

直接通过类本身来访问静态成员

 

例如:

public class Book() {
    public static final int PAGES = 100;
}

 

//不推荐
Book book = new Book();
int pages = book.PAGES;
//推荐
int pages = Book.PAGES;

 

 

原文博主:https://blog.csdn.net/testzhou2012/article/details/79862898

posted @ 2019-04-07 16:23  MK_筱雨  阅读(1523)  评论(0编辑  收藏  举报