In Java, there are four ways to instantiate classes:
1) use the new operator
2) Call the newInstance () method of the Class Object
3) Call the clone () method to copy existing instances.
4) deserialization class through the readObject () method of ObjectInputStream
1. ClassInstance. java
- Import java. io .*;
- Class ClassInstance implements Cloneable, Serializable {
- Private String str = "test ...";
- Public void fun (){
- System. out. println (str );
- }
- Public ClassInstance (String str ){
- System. out. println ("instantiation of classes ");
- This. str + = str;
- }
- Public ClassInstance (){
- System. out. println ("instantiation of the class without Parameters ");
- }
- Public Object clone (){
- Return this;
- }
- }
2. ClassInstanceTest. java
- Import java. io .*;
- Import java. lang. reflect .*;
- Public class ClassInstanceTest {
- Public static void main (String [] args) throws ClassNotFoundException, InstantiationException,
- IllegalAccessException, IOException, InvocationTargetException, NoSuchMethodException {
- // The first type of Instantiation Method
- ClassInstance ci01 = new ClassInstance ("01 ");
- Ci01.fun ();
- // The second type of Instantiation Method
- ClassInstance ci02 = (ClassInstance) Class. forName ("ClassInstance"). newInstance ();
- Ci02.fun ();
- // The third type of Instantiation Method
- ClassInstance ci03 = (ClassInstance) ci01.clone ();
- Ci03.fun ();
- // The fourth type of Instantiation Method
- FileOutputStream fos = new FileOutputStream ("ci. tmp ");
- ObjectOutputStream oos = new ObjectOutputStream (fos );
- Oos. writeObject (ci01 );
- Oos. close ();
- Fos. close ();
- FileInputStream FCM = new FileInputStream ("ci. tmp ");
- ObjectInputStream ois = new ObjectInputStream (FCM );
- ClassInstance ci04 = (ClassInstance) ois. readObject ();
- Ois. close ();
- FCM. close ();
- Ci04.fun ();
- System. out. println ("-------------------- additional test --------------------");
- ClassInstance ci05 = null;
- // Additional thinking: is there a way to implement a parameter-based constructor In the second type of Instantiation?
- // Obtain the Class Construction Information
- Constructor [] ctor = Class. forName ("ClassInstance"). getDeclaredConstructors ();
- // Find the required Constructor
- For (int I = 0; I <ctor. length; I ++ ){
- Class [] cl = ctor [I]. getParameterTypes ();
- If (cl. length = 1 ){
- // Instantiate the object
- Ci05 = (ClassInstance) Class. forName ("ClassInstance"). getConstructor (cl). newInstance (new Object [] {"05 "});
- }
- }
- Ci05.fun ();
- }
- }
3. output results
- Instantiation of a parameter class
- Test... 01
- Instantiate a class without Parameters
- Test...
- Test... 01
- Test... 01
- ------------------- Additional test --------------------
- Instantiation of a parameter class
- Test... 05
In addition to these situations, you can instantiate a Java class object. The implicit call instantiation also utilizes the previous concentration. For example, common methods:
- public class ClassInstance{
- public ClassInstance(){
- }
- public ClassInstance getInstance(){
- return new ClassInstance();
- }
- }
Through observation, we find that no matter which method must go through a step-call the constructor method. No matter how the constructor acts as the initialization class, the meaning will not change.