Instantiate objects without using new operator

2020-02-08 21:47发布

In one of the java interview, the following question is asked:

In java is there a way to instantiate an object without using new operator? I replied to him that there is no other way of instantiation. But he asked me how an object in java is instantiated with the configurations in an xml file in java(in spring framework). I said, internally the spring uses reflection utils to create an object with a new operator. But the interviewer was not convinced with my answer.

I saw this link to be useful but there is a new operator indirectly involved in one or the other internal methods.

Is there really a way to instantiate objects in java without using new operator?

8条回答
兄弟一词,经得起流年.
2楼-- · 2020-02-08 21:56

AFAIK, Class.newInstance() and Constructor.newInstance() don't use the new keyword internally.

Other ways to create an instance without the new keyword:

  • Object.clone()
  • Serialization
查看更多
我命由我不由天
3楼-- · 2020-02-08 21:56

There are only three standard ways to instantiate a class without the use of the new operator, and they are as follows:

  1. newInstance()
  2. clone()
  3. De-serialization
查看更多
该账号已被封号
4楼-- · 2020-02-08 22:04

You can use clone method to create a copy of object without new operator.

clone is used to make a copy of object. There are certain things which you should keep in mind while using clone method of Object.

  • implement "Cloneable" interface to each class which you want to clone. If a class does not implement "Cloneable" interface, clone method will throw "CloneableNotSupportedException". This interface does not contain any methods. It is just a marker interface.

Example for String class

String sample = new String();

Now we are not going to use new operator and we will create a new object

String sampleClone = sample.clone();

Other you can use Class.forName()

public static Class forName(String className) throws ClassNotFoundException

Returns the Class object associated with the class or interface with the given string name.

Example -- Class exampleClass = Class.forName(yourtClass);

Read official docs

http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#forName%28java.lang.String%29

查看更多
爷的心禁止访问
5楼-- · 2020-02-08 22:07

An array initializer of the following form does not use new explicitly.

int ia[][] = { {1, 2}, null };

This creates an object ... by autoboxing:

Integer big = 9999;

Finally, the following result in the creation of objects somewhere in the program's lifecycle :-)

Class c = ThisClass.class;
String s = "a literal";
enum SomeEnum { WON, CHEW, TREE }

(And there are many, many ways to do it using library methods ... or native code)


Underneath the covers, any creation of a new object in pure Java involves either the new bytecode, or one of 3 new array bytecodes. That probably includes all of my examples.

Interestingly, Object.clone() and ObjectInputStream.readObject() both use "magic" mechanisms for creating instances that don't involve the above bytecodes, and don't call constructors in the normal way.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2020-02-08 22:09

You can deserialize an object without invoking new.

Ok, you have to call new on the FileInputStream and the ObjectInputStream, but I assume that is fair use.

 FileInputStream fis = new FileInputStream("myClassInstance.ser");
 ObjectInputStream ois = new ObjectInputStream(fis);
 MyClass myObject = (MyClass)ois.readObject();
查看更多
在下西门庆
7楼-- · 2020-02-08 22:10

You can do it using the Java Reflection API. That's how the Spring framework's DI works (instantiating object from xml).

Class<YourClass> c = YourClass.class;
YourClass instance = c.newInstance();

Also, Considering enum to be a special class, the instances of the enum are created without using new Operator.

public enum YourEnum { X, Y }
查看更多
登录 后发表回答