Creating objects without using a new operator

In a java interview, the following question is asked:

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

I saw this link to be useful, but there is a new operator indirectly associated with one or other internal methods.

Is there a way to create objects in java without using the new operator?

+6
source share
8 answers

You can do this using the Java Reflection API . This is how Spring framework DI works (object instance from xml).

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

Also, given that enum is a special class , enumeration instances are created without using the new Operator.

 public enum YourEnum { X, Y } 
+9
source

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

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

This creates an object ... by autoboxing:

 Integer big = 9999; 

Finally, the following result in creating objects somewhere in the program’s life cycle :-)

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

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


Under covers, any creation of a new object in pure Java includes either the new bytecode, or one of the 3 bytes of t new array . This probably includes all my examples.

Interestingly, Object.clone() and ObjectInputStream.readObject() use "magic" mechanisms to create instances that are not related to the bytecodes mentioned above and do not call constructors in the usual way.

+9
source

you can use jdbc path

 Class.forName("YOURCLASSNAME").newInstance() 
+5
source

You can use the clone method to create a copy of an object without a new statement.

clone is used to create a copy of the object. There are certain things you should keep in mind when using the object cloning method.

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

Example for class String

 String sample = new String(); 

Now we will not use the 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 a ClassNotFoundException

Gets the class object associated with the class or interface with the specified string name.

Example. Class exampleclass = Class.forName (yourtClass);

Read white papers

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

+1
source
  import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectCreateDifferentWays { public static void main(String[] args) throws Exception { ///1st Way with newInstance() Class cls = Class.forName("Student"); Student ob1 = (Student) cls.newInstance(); ob1.display(); ///2nd Way with new Operator Student ob2 = new Student(); ob2.display(); //3rd Way with clone Student ob3 = (Student) ob2.clone(); ob3.display(); //4th Way with Deserialization FileOutputStream out = new FileOutputStream("file.txt"); ObjectOutputStream obOut = new ObjectOutputStream(out); obOut.writeObject(ob2); obOut.flush(); FileInputStream fIn = new FileInputStream("file.txt"); ObjectInputStream obIn = new ObjectInputStream(fIn); Student ob4 = (Student) obIn.readObject(); ob4.display(); } } class Student implements Cloneable, Serializable { public void display() throws Exception { System.out.println("Display "); }@ Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } } 

There are several ways to create an object in Java 1) the newInstance () method 2) the new operator 3) the clone () method 4) Deserialization

+1
source

You can deserialize an object without calling a new one.

Well, you should call new on FileInputStream and ObjectInputStream , but I assume this is a fair use.

  FileInputStream fis = new FileInputStream("myClassInstance.ser"); ObjectInputStream ois = new ObjectInputStream(fis); MyClass myObject = (MyClass)ois.readObject(); 
0
source

AFAIK, Class.newInstance () and Constructor.newInstance () do not use the new keyword inside themselves.

Other ways to instantiate without a new keyword:

  • Object.clone ()
  • Serialization
0
source

There are only three standard ways to instantiate a class without using a new operator, and they look like this:

  • newInstance ()
  • clone()
  • Deserialization
0
source

Source: https://habr.com/ru/post/945256/


All Articles