我有一个类,我在该类中为同一个类对象进行同步锁定.
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中的方法的实际用法.
对于前者
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 ×2