1. Create objects with the new statement, which is the most common way to create objects.
2. Using the Reflection method, call the Java.lang.Class or Java.lang.reflect.Constructor class newinstance () instance methods.
3. Call the object's clone () method.
4. Invoke the ReadObject () method of the Java.io.ObjectInputStream object using the deserialization means.
I. Creating an object with the new statement
Java code
- User user = new user ()
Two. Use of reflective means
(1) Call Java.lang.Class.newInstance ()
Java code
- Class.forName (classname). newinstance ()
- Wife Wife = (Wife) class.forname ("Com.java.clone.Wife"). newinstance ();
(2) Call the newinstance () of the Java.lang.reflect.Constructor class
Java code
- Constructor Constructor = Wife.class. Getdeclaredconstructor (Int.class,string.class);
- Wife Wife = (Wife) constructor.newinstance (1, "Nihao");
Three. Calling the object's clone () method
Java code
- Wife Wife = new Wife (1, "Wang");
- Wife wife2 = null;
- Wife2 = (Wife) wife.clone ();//Use the Clone () method to produce a new object
Methods for creating objects in Java