3

I saw the below code under practice questions in SCJP book

Object obj = new Object();

First I thought it might be an error since I didn't use statement like this. Now I understand it is a valid statement.

I just want to understand what is the practical use of this if any. What can you use an object of the Object type (rather than of a class derived from Object) for?

6
  • 1
    How else would you create a new instance of the Object class? Commented Sep 9, 2014 at 15:21
  • @JustinNiessner I believe he is asking what is the usefulness of creating a new instance of the Object class.
    – Mason T.
    Commented Sep 9, 2014 at 15:22
  • 1
    @JustinNiessner Why can't Object be an interface instead of concrete class? Since it is a class in Java what is the use of being a class instead of interface?
    – Athiruban
    Commented Sep 9, 2014 at 15:27
  • 1
    @Athiruban Object provides implementation for methods such as #equals, and only classes provide implementations, interfaces cannot. classes that do not specify a parent inherit from object, so they all inherit these methods. This is important - e.g. for collections - as testing if the object is in the collection can never be left undefined
    – James
    Commented Sep 9, 2014 at 15:31
  • @James, Object provides default implementations for methods such as #equals. An interface would work if nobody expected those methods to have default implementations. I think the real reason is historical: The roots of Java's Object and Class classes go all the way back to Smalltalk---way before anybody ever thought of interface. Commented Sep 9, 2014 at 16:04

4 Answers 4

16

The code declares a reference of type Object, and assigns a newly allocated Object instance to it.

Now, instances of class Object might seem pointless. However, they do have practical uses. One such use is locking:

...
synchronized (obj) {
   ...
}
...
1
  • Only answer that has anything to do with the question. +1 Commented Sep 9, 2014 at 15:23
1

It can be used for example for synchronization:

synchronized (obj) {
   ... 
}
1

A practical use is not so common (NPE answer is one example), but since every class inherits from Object, when you call its constructor, the constructor of Object class will be implicitly called at some time, so you will need the capability to construct a new Object.

public class Foo {   // implicitly inherits from Object
    public Foo() {
        super(); // Object default constructor call
    }
}

public class Bar extends Foo {
    public Bar() {
        super(); // Foo default constructor call
    }
}

Bar b = new Bar();  // new Bar() calls new Foo() that calls new Object()

It's important to note that the calls of super() (superclass constructor) will be inserted implicutly by compiler.

1
  • Thanks for reminding me that still we are implicitly using Object's constructor. It didn't strike me when I saw the question in SCJP book.
    – Athiruban
    Commented Sep 9, 2014 at 15:46
0

With the new operator, you allocate memory for the object, and call its constructor. Usually creating an instance of an Object class wont appear much in day to day coding. Its also important to know that every class you make extends from object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.