小编Raj*_*esh的帖子

可以局部覆盖最终方法吗?

public class Test  {  

    public static void main(String args[]){
        Test t = new Test();
        t.inner();

    }
    public final void print() { 
        System.out.print("main"); 
    }      

    public void inner() {       
        class TestInner {           
            void print(){
                System.out.print("sub");                
            }

        }
        TestInner inner =new TestInner();
        inner.print();
        print();
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:子域

问题:类Test中的print()方法是final是可以访问的本地类,但是本地类仍然能够再次定义print()方法如何?

如果我们在超类中声明私有final x(),它在子类中是不可访问的,所以我们可以在子类中定义x().

如果我们在超类中声明public final x(),它可以在子类中访问,所以我们不能在子类中定义x().

谁能解释一下?

java methods final

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

可以在编译时评估表达式吗?

public class Test1 {
    static final int i;
    static{
        if(3<2){
        i = 0;
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)
public class Test2 {
    static final int i;
    static{
        if(3>2){
        i = 0;
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

Test1编译失败,Class Test2编译成功.

任何人都可以告诉我编译器如何能够在if条件下评估表达式吗?

java static final

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

最后块会在主线程以外的线程中执行吗?

public class Test {
    public static void main(String args[]) throws Exception{
        try{        
            System.out.print("1");
            throw new Exception("first");
        }   
        catch (Exception e) {
            System.out.print("2");
            throw new Exception("second");      
        }
        **finally**{
            System.out.print("3");
            try{
                System.out.print("4");              
            }catch (Exception e) {
                System.out.print("5");
                throw new Exception("third");
            }
            finally{
                System.out.print("6 ");             
            }
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

首次输出:

12Exception in thread "main" 346 java.lang.Exception: second
    at src.dec.TST501.main(TST501.java:11)
Run Code Online (Sandbox Code Playgroud)

第二轮输出:

12346 Exception in thread "main" java.lang.Exception: second
    at src.dec.TST501.main(TST501.java:11)
Run Code Online (Sandbox Code Playgroud)

第三次运行输出:1线程"main"中的异常java.lang.Exception:src.dec.TST501.main中的第二个2346(TST501.java:11)

任何人都可以解释一下它是如何发生的吗?finally块是否会在除main之外的任何其他线程中执行?

java multithreading

0
推荐指数
1
解决办法
178
查看次数

标签 统计

java ×3

final ×2

methods ×1

multithreading ×1

static ×1