3

In Java, every class implicitly extends the Object class. So, does this mean we can create an object of the Object class ?


public static void main(String[] args) {

Object ob=new Object();
    // code here ....
 }

When I tried it, it compiled and ran successfully. In that case, can someone please explain when do we generally create an object of the Object class ?

3
  • To boxed an object. i.e to hold the reference of sub-class object. Commented Sep 24, 2012 at 2:08
  • You can use it as a sort of mixed-type array: ideone.com/fojc4 Commented Sep 24, 2012 at 2:09
  • If by 'create', you mean actually instantiate one the way you did, by invoking new Object(), generally, you don't. I've been programming in Java for a long time, and I don't think I've ever written code that does that. Commented Sep 24, 2012 at 2:11

4 Answers 4

7

You could instantiate an instance of an Object if you want to do a synchronization lock.

public void SomeClass {
    private Object lock = new Object();
    private SomeState state;

    public void mutateSomeSharedState() {
        synchronized(lock) {
            //mutate some state
        }
    }

    public SomeState readState() {
        synchronized(lock) {
            //access state
        }
    }
}

It might be necessary to do this when this is already used to lock some other state of the same object, or if you want to have your lock be private (ie, no one else can utilize it). Even if it isn't necessary, some people prefer to do things that way. This is merely an example of when someone might do it.

Sign up to request clarification or add additional context in comments.

1 Comment

You might want to explain why using new Object is a better idea than using this, or some other arbitrary object.
2

Normally we don't create an object of the Object class directly. Usually, we create instances of direct/indirect subclasses of Object.

A scenario where we create an instance of Object is to create an object to synchronize threads.

Eg:

   Object lock = new Object();
   //...
   synchronize( lock ) {
       //...
       //...
   }

However the Object class is used a lot to describe parameters of methods and of instance variables that may assume values of different classes (polymorphism).

Eg:

void example(Object arg) {
   // ...
   System.out.println( "example=" + arg.toString() );
}

Object foo = returnObject();

Sometimes the use of Generics may be better than using Object to describe parameters and variables.

Comments

0

For the most part I believe Object is no longer used explicitly.

Since Java's debut of Generics, casting to the Object class is almost non-existent.

Comments

0

Since java.lang.Object is the super most class, it can be substituted with any instance we create. This concept is very useful when you not aware of the type( eg: A method which conditionally returns different types , Collections with multiple types)

Also commonly used when you want to instantiate class from String,or execute a method using reflections.

However, direct usage of Object is getting redundant due to Generics. Cheers Satheesh

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.