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 20:13
  • using the new operator (thus invoking a constructor)
  • using reflection clazz.newInstance() (which again invokes the constructor). Or by clazz.getConstructor(..).newInstance(..) (again using a constructor, but you can thus choose which one)

To summarize the answer - one main way - by invoking the constructor of the object's class.

Update: Another answer listed two ways that do not involve using a constructor - deseralization and cloning.

查看更多
宁负流年不负卿
3楼-- · 2018-12-31 20:14

Other ways if we are being exhaustive.

  • On the Oracle JVM is Unsafe.allocateInstance() which creates an instance without calling a constructor.
  • Using byte code manipulation you can add code to anewarray, multianewarray, newarray or new. These can be added using libraries such as ASM or BCEL. A version of bcel is shipped with Oracle's Java. Again this doesn't call a constructor, but you can call a constructor as a seperate call.
查看更多
零度萤火
4楼-- · 2018-12-31 20:15

Yes, you can create objects using reflection. For example, String.class.newInstance() will give you a new empty String object.

查看更多
浮光初槿花落
5楼-- · 2018-12-31 20:15

Depends exactly what you mean by create but some other ones are:

  • Clone method
  • Deserialization
  • Reflection (Class.newInstance())
  • Reflection (Constructor object)
查看更多
墨雨无痕
6楼-- · 2018-12-31 20:16

You can also clone existing object (if it implements Cloneable).

Foo fooClone = fooOriginal.clone (); 
查看更多
旧时光的记忆
7楼-- · 2018-12-31 20:16

Method 1

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

Employee object = new Employee();

Method 2

Using Class.forName(). Class.forName() gives you the class object, which is useful for reflection. The methods that this object has are defined by Java, not by the programmer writing the class. They are the same for every class. Calling newInstance() on that gives you an instance of that class (i.e. callingClass.forName("ExampleClass").newInstance() it is equivalent to calling new ExampleClass()), on which you can call the methods that the class defines, access the visible fields etc.

Employee object2 = (Employee) Class.forName(NewEmployee).newInstance();

Class.forName() will always use the ClassLoader of the caller, whereas ClassLoader.loadClass() can specify a different ClassLoader. I believe that Class.forName initializes the loaded class as well, whereas the ClassLoader.loadClass() approach doesn’t do that right away (it’s not initialized until it’s used for the first time).

Another must read:

Java: Thread State Introduction with Example Simple Java Enum Example

Method 3

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

Employee secondObject = new Employee();
Employee object3 = (Employee) secondObject.clone();

Method 4

Using newInstance() method

Object object4 = Employee.class.getClassLoader().loadClass(NewEmployee).newInstance();

Method 5

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

// Create Object5
// create a new file with an ObjectOutputStream
FileOutputStream out = new FileOutputStream("");
ObjectOutputStream oout = new ObjectOutputStream(out);

// write something in the file
oout.writeObject(object3);
oout.flush();

// create an ObjectInputStream for the file we created before
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("crunchify.txt"));
Employee object5 = (Employee) ois.readObject();
查看更多
登录 后发表回答