What are all the different ways to create an objec

2018-12-31 19:51发布

Had a conversation with a coworker the other day about this.

There's the obvious which is to use a constructor, but what other ways are there?

标签: java
22条回答
弹指情弦暗扣
2楼-- · 2018-12-31 19:53

From an API user perspective, another alternative to constructors are static factory methods (like BigInteger.valueOf()), though for the API author (and technically "for real") the objects are still created using a constructor.

查看更多
不流泪的眼
3楼-- · 2018-12-31 19:55

There are four different ways to create objects in java:

A. Using new keyword
This is the most common way to create an object in java. Almost 99% of objects are created in this way.

 MyObject object = new MyObject();

B. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.

MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

C. Using clone()
The clone() can be used to create a copy of an existing object.

MyObject anotherObject = new MyObject();
MyObject object = (MyObject) anotherObject.clone();

D. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.

ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();

You can read from here

查看更多
残风、尘缘若梦
4楼-- · 2018-12-31 19:55

There are five different ways to create an object in Java,

1. Using new keyword → constructor get called

Employee emp1 = new Employee();

2. Using newInstance() method of Class → constructor get called

Employee emp2 = (Employee) Class.forName("org.programming.mitra.exercises.Employee")
                                .newInstance();

It can also be written as

Employee emp2 = Employee.class.newInstance();

3. Using newInstance() method of Constructor → constructor get called

Constructor<Employee> constructor = Employee.class.getConstructor();
Employee emp3 = constructor.newInstance();

4. Using clone() method → no constructor call

Employee emp4 = (Employee) emp3.clone();

5. Using deserialization → no constructor call

ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
Employee emp5 = (Employee) in.readObject();

First three methods new keyword and both newInstance() include a constructor call but later two clone and deserialization methods create objects without calling the constructor.

All above methods have different bytecode associated with them, Read Different ways to create objects in Java with Example for examples and more detailed description e.g. bytecode conversion of all these methods.

However one can argue that creating an array or string object is also a way of creating the object but these things are more specific to some classes only and handled directly by JVM, while we can create an object of any class by using these 5 ways.

查看更多
刘海飞了
5楼-- · 2018-12-31 19:55

This should be noticed if you are new to java, every object has inherited from Object

protected native Object clone() throws CloneNotSupportedException;

查看更多
长期被迫恋爱
6楼-- · 2018-12-31 19:55

There is a type of object, which can't be constructed by normal instance creation mechanisms (calling constructors): Arrays. Arrays are created with

 A[] array = new A[len];

or

 A[] array = new A[] { value0, value1, value2 };

As Sean said in a comment, this is syntactically similar to a constructor call and internally it is not much more than allocation and zero-initializing (or initializing with explicit content, in the second case) a memory block, with some header to indicate the type and the length.

When passing arguments to a varargs-method, an array is there created (and filled) implicitly, too.

A fourth way would be

 A[] array = (A[]) Array.newInstance(A.class, len);

Of course, cloning and deserializing works here, too.

There are many methods in the Standard API which create arrays, but they all in fact are using one (or more) of these ways.

查看更多
不流泪的眼
7楼-- · 2018-12-31 20:03
登录 后发表回答