Is there a legal use for an object constructor?

What will be the legal use of the following code?

Object o =new Object(); 

From what I understand, this object does not make sense and does not contain real data (except, perhaps, its hash code). Why use this? This is an acceptable practice. If I can do this, I can explicitly extend the class of the object.

+6
source share
1 answer

From what I understand, this object does not make sense and does not contain real data (except, perhaps, its hash code)

The object carries its identity and its monitor . That's why this assignment is used to create object monitors that are separate from the object itself.

Why use this? Is this an acceptable practice?

The only thing I saw for this was to use the object as a monitor for other objects.

If I can do this, can I explicitly extend the class of the object?

That's right. You can extend an object in an anonymous class, for example:

 Object obj = new Object() { @Override public String toString() { return "Hello, world!"; } }; 
+8
source

All Articles