Java: Rationale of the Object class not being declared abstract

后端 未结 10 1100
粉色の甜心
粉色の甜心 2020-12-06 04:54

Why wasn\'t the java.lang.Object class declared to be abstract ?

Surely for an Object to be useful it needs added state or behaviour, an Object class is

10条回答
  •  没有蜡笔的小新
    2020-12-06 05:23

    You can instantiate Object for synchronization locks:

    Object lock = new Object();
    
    void someMethod() {
      //safe stuff
      synchronized(lock) {
         //some code avoiding race condition
      }
    }
    
    void someOtherMethod() {
      //safe code
      synchronized(lock) {
        //some other stuff avoiding race condition
      }
    }
    

提交回复
热议问题