6

What would the legitimate use of the following code be?

Object o =new Object();

From what I understand this object has no use and carries no real data (except for maybe its hash code). Why would this be used? Is it acceptable practice. If I am able to do this could I explicitly extend the object class.

  • 7
    I've seen it used to create an unambiguous locking object for threads, although there are far better ways to do concurrency. – Hovercraft Full Of Eels Jun 24 '16 at 00:36
  • Ideally shouldn't you be using an AtomicReference foo; – Rohan Jhunjhunwala Jun 24 '16 at 00:39
  • 1
    Why couldn't you explicitly extend `Object`? That's the *implicit* default. – Elliott Frisch Jun 24 '16 at 00:39
  • So is it completely valid code to say public class MyClass extends Object{} – Rohan Jhunjhunwala Jun 24 '16 at 00:42
  • Yes, you can do `extends Object` (which is the default if there is no superclass given). – Thilo Jun 24 '16 at 00:43
  • Yes, when you write `public class MyClass` you are actually telling the compiler `public class MyClass extends Object`. Java lets you omit `extends Object`, but it's actually there. – Sergey Kalinichenko Jun 24 '16 at 00:43
  • Correct its a waste of space to create plain object instances. If lock is needed then we have Lock objects in newer Java versions. Although you should know that for every object that is created the above object is implicitly created (due to empty constructor), for example when you create any instance with new keyword, the parent instances will also be created in the respective order and ending on Object class. – Amit Mahajan Jun 24 '16 at 01:16
  • 1
    @amitmah: There are no separate "parent instances". Everything contributed by parent classes is folded into a single object instance. – Thilo Jun 24 '16 at 02:03
  • I've occasionally done this in test-code for containers to make it clear that the type of some data-input did not matter at all in some specific context where *any* non-null object was acceptable (or not - if testing for some type-mismatch etc.). – Hulk Jun 24 '16 at 06:58
  • @Thilo this is nice info, do you have any code snippet or any documentation to check this. I never came across any object folding use case before :) – Amit Mahajan Jun 30 '16 at 01:26

1 Answers1

8

From what I understand this object has no use and carries no real data (except for maybe its hash code)

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

Why would this be used? Is it acceptable practice?

The only use that I've seen for this making an object to be used as a monitor for other objects.

If I am able to do this could I explicitly extend the object class?

Absolutely. You can extend an object in an anonymous class, like this:

Object obj = new Object() {
    @Override
    public String toString() {
        return "Hello, world!";
    }
};
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523