小编C J*_*C J的帖子

如何在Java中关闭隐式Stream?

Files.walk是我应该关闭的流之一,但是,如何在下面的代码中关闭流?下面的代码是否有效,或者我是否需要重写它以便我可以访问流来关闭它?

List<Path> filesList = Files.walk(Paths.get(path)).filter(Files::isRegularFile ).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)

java java-8 try-with-resources java-stream

5
推荐指数
1
解决办法
81
查看次数

为什么我可以调用私有方法?

我不应该能够调用实例化对象的私有方法.我想知道为什么下面的代码有效.

public class SimpleApp2 {
    /**
     * @param args
     */
    private int var1;

    public static void main(String[] args) {
        SimpleApp2 s = new SimpleApp2();
        s.method1(); // interesting?!
    }

    private void method1() {
        System.out.println("this is method1");
        this.method2(); // this is ok
        SimpleApp2 s2 = new SimpleApp2();
        s2.method2(); // interesting?!
        System.out.println(s2.var1); // interesting?!
    }

    private void method2() {
        this.var1 = 10;
        System.out.println("this is method2");
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道可以从类中访问私有方法.但是,如果类中的方法实例化同一个类的对象,那么范围规则是否应该应用于该实例化对象?

可以像main这样的静态方法访问类的非静态成员,如本例所示?

java core

3
推荐指数
1
解决办法
5488
查看次数

标签 统计

java ×2

core ×1

java-8 ×1

java-stream ×1

try-with-resources ×1