Java instantiation Class Method

Source: Internet
Author: User

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

 
  1. Import java. io .*;
  2. Class ClassInstance implements Cloneable, Serializable {
  3. Private String str = "test ...";
  4. Public void fun (){
  5. System. out. println (str );
  6. }
  7. Public ClassInstance (String str ){
  8. System. out. println ("instantiation of classes ");
  9. This. str + = str;
  10. }
  11. Public ClassInstance (){
  12. System. out. println ("instantiation of the class without Parameters ");
  13. }
  14. Public Object clone (){
  15. Return this;
  16. }
  17. }

2. ClassInstanceTest. java

 
  1. Import java. io .*;
  2. Import java. lang. reflect .*;
  3. Public class ClassInstanceTest {
  4. Public static void main (String [] args) throws ClassNotFoundException, InstantiationException,
  5. IllegalAccessException, IOException, InvocationTargetException, NoSuchMethodException {
  6. // The first type of Instantiation Method
  7. ClassInstance ci01 = new ClassInstance ("01 ");
  8. Ci01.fun ();
  9. // The second type of Instantiation Method
  10. ClassInstance ci02 = (ClassInstance) Class. forName ("ClassInstance"). newInstance ();
  11. Ci02.fun ();
  12. // The third type of Instantiation Method
  13. ClassInstance ci03 = (ClassInstance) ci01.clone ();
  14. Ci03.fun ();
  15. // The fourth type of Instantiation Method
  16. FileOutputStream fos = new FileOutputStream ("ci. tmp ");
  17. ObjectOutputStream oos = new ObjectOutputStream (fos );
  18. Oos. writeObject (ci01 );
  19. Oos. close ();
  20. Fos. close ();
  21. FileInputStream FCM = new FileInputStream ("ci. tmp ");
  22. ObjectInputStream ois = new ObjectInputStream (FCM );
  23. ClassInstance ci04 = (ClassInstance) ois. readObject ();
  24. Ois. close ();
  25. FCM. close ();
  26. Ci04.fun ();
  27. System. out. println ("-------------------- additional test --------------------");
  28. ClassInstance ci05 = null;
  29. // Additional thinking: is there a way to implement a parameter-based constructor In the second type of Instantiation?
  30. // Obtain the Class Construction Information
  31. Constructor [] ctor = Class. forName ("ClassInstance"). getDeclaredConstructors ();
  32. // Find the required Constructor
  33. For (int I = 0; I <ctor. length; I ++ ){
  34. Class [] cl = ctor [I]. getParameterTypes ();
  35. If (cl. length = 1 ){
  36. // Instantiate the object
  37. Ci05 = (ClassInstance) Class. forName ("ClassInstance"). getConstructor (cl). newInstance (new Object [] {"05 "});
  38. }
  39. }
  40. Ci05.fun ();
  41. }
  42. }

3. output results

 
  1. Instantiation of a parameter class
  2. Test... 01
  3. Instantiate a class without Parameters
  4. Test...
  5. Test... 01
  6. Test... 01
  7. ------------------- Additional test --------------------
  8. Instantiation of a parameter class
  9. 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:

 
  1. public class ClassInstance{  
  2.  public ClassInstance(){  
  3.  }  
  4.      public ClassInstance getInstance(){  
  5.  return new ClassInstance();  
  6.  }  
  7.  } 

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.