简体   繁体   English

在Java中创建Object类的对象

[英]Creating an object of the Object class in Java

In Java, every class implicitly extends the Object class. 在Java中,每个类都隐式扩展了Object类。 So, does this mean we can create an object of the Object class ? 那么,这是否意味着我们可以创建Object类的对象?


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 ? 在这种情况下,有人可以解释我们何时通常创建Object类的对象?

You could instantiate an instance of an Object if you want to do a synchronization lock. 如果要进行同步锁定,可以实例化Object的实例。

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). this已经被用来锁定同一个对象的某个其他状态,或者如果你想让你的锁是私有的(即,没有其他人可以使用它)时,可能需要这样做。 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. 这仅仅是有人可能会这样做的一个例子。

Normally we don't create an object of the Object class directly. 通常我们不直接创建Object类的对象。 Usually, we create instances of direct/indirect subclasses of Object. 通常,我们创建Object的直接/间接子类的实例。

A scenario where we create an instance of Object is to create an object to synchronize threads . 我们创建Object实例的场景是创建一个同步线程的对象。

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 ). 但是,Object类用于描述方法和实例变量的参数,这些参数可以假设不同类的值( 多态 )。

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. 有时使用泛型可能比使用Object描述参数和变量更好。

For the most part I believe Object is no longer used explicitly. 在大多数情况下,我认为不再明确使用Object

Since Java's debut of Generics, casting to the Object class is almost non-existent. 自从Java首次出现泛型以来,转换为Object类几乎不存在。

Since java.lang.Object is the super most class, it can be substituted with any instance we create. 由于java.lang.Object是最多的类,因此它可以替换为我们创建的任何实例。 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. 当您想要从String实例化类或使用反射执行方法时,也常用。

However, direct usage of Object is getting redundant due to Generics. 但是,由于泛型,直接使用Object变得多余。 Cheers Satheesh 干杯Satheesh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM