0
点赞
收藏
分享

微信扫一扫

IDEA提示,git commit 提示警告

炽凤亮尧 2022-02-15 阅读 58

Can be replaced with method reference less 可以用代码更少的方法引用代替

// 该写法会提示该警告
list.forEach(s -> System.out.println(s));
        
// 使用方法引用消除警告,一般 alt+enter 可以自动转换
list.forEach(System.out::println);

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;
举报

相关推荐

0 条评论