小编Sah*_*hil的帖子

在同步块中锁定自己的对象的问题

我有一个类,我在该类中为同一个类对象进行同步锁定.

class Demo{
    private Demo obj=new Demo();
    synchronized(obj){
         //some code here
    }
}
Run Code Online (Sandbox Code Playgroud)

然后它给出了堆栈溢出的错误,但我写了相同的代码

class Demo{
    private static Demo obj=new Demo();
    synchronized(obj){
         //some code here
    }
}
Run Code Online (Sandbox Code Playgroud)

然后它工作正常所以请任何人在那里逻辑地向我解释这里发生了什么.

java

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

什么方法隐藏实际使用?

我想知道隐藏在java中的方法的实际用法.

对于前者

class MyClass{
     public static void myMethod(){
         //some code here
     }
}

class MyClass1 extends MyClass{

     public static void myMethod(){
         //some code here
     }
  public static void main(String args[]){
    MyClass1 obj1=new MyClass1();
    MyClass obj=obj1;
    MyClass.myMethod();  // this method will run the parent Class method myMethod then what is the use of this practically.                           
    }
}
Run Code Online (Sandbox Code Playgroud)

java

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

标签 统计

java ×2