Methods for creating objects in Java

Source: Internet
Author: User

There are 4 ways to explicitly create an object:

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.

The following illustrates the process of creating an object in the previous 3 ways.

[Java]View PlainCopy
  1. Public class Customer implements cloneable{
  2. private String name;
  3. private int age;
  4. Public Customer () {
  5. This ("Unknown",0);
  6. System.out.println ("call default constructor");
  7. }
  8. Public Customer (String name,int age) {
  9. This.name=name;
  10. This.age=age;
  11. System.out.println ("call second constructor");
  12. }
  13. Public Object Clone ()throws clonenotsupportedexception{
  14. return Super.clone ();
  15. }
  16. Public Boolean equals (Object o) {
  17. if (this==o)return true;
  18. if (! (o instanceof Customer))  return false;
  19. Final Customer other= (customer) O;
  20. if (this.name.equals (other.name) && this.age==other.age)
  21. return true;
  22. Else
  23. return false;
  24. }
  25. Public String toString () {
  26. return "name=" +name+", age=" +age;
  27. }
  28. Public static void Main (String args[])throws exception{
  29. Using reflection to create a customer object
  30. Class objclass=class.forname ("Customer");
  31. Customer c1= (Customer) objclass.newinstance (); //Invokes the default construction method of the Customer class
  32. System.out.println ("C1:" +C1); //Print name=unknown,age=0
  33. Create a customer object with the new statement
  34. Customer c2=New Customer ("Tom",20);
  35. System.out.println ("C2:" +C2); //Print name=tom,age=20
  36. Creating a Customer object using cloning
  37. Customer c3= (Customer) C2.clone (); //Does not invoke the construction method of the Customer class
  38. System.out.println ("C2==C3:" + (C2==C3)); //Print false
  39. System.out.println ("C2.equals (C3):" +c2.equals (C3)); //Print True
  40. System.out.println ("C3:" +C3); //Print name=tom,age=20
  41. }
  42. }

The printing results of the above program are as follows:

Call second constructor

Call default constructor

C1:name=unknown,age=0

Call second constructor

C2:name=tom,age=20

C2==c3:false

C2.equals (C3): True

C3:name=tom,age=20

As you can see from the above printout, when you create a customer object with the Newinstance () method of the new statement or class object, the constructor of the customer class is executed, and when you create the customer object with the object's clone () method, The construction method of the customer class is not executed. Difference

In addition to the above 4 ways of explicitly creating objects, you can implicitly create objects in your program, including the following scenarios:

1. For each command-line argument in the Java command, the Java virtual machine creates the corresponding string object, organizes it into a string array, and passes the array as a parameter to the program entry main (String args[]) method.

2. The direct number of the string type in the program code corresponds to a String object, for example:

[Java]View PlainCopy
    1. String s1="Hello";
    2. String s2="Hello"; //s2 and S1 reference the same string object
    3. String s3=new String ("Hello");
    4. System.out.println (S1==S2); //Print True
    5. System.out.println (S1==S3); //Print false

After executing the above program, there are actually only two string objects in memory, one is the direct number, implicitly created by the Java Virtual machine, and one is explicitly created by the new statement.

3. The operation result of the string operator "+" is a new string object. For example:

[Java]View PlainCopy
    1. String s1="H";
    2. String s2="Ello";
    3. String s3=s1+s2; //S3 refers to a new string object
    4. System.out.println (s3=="Hello"); //Print false
    5. System.out.println (S3.equals ("Hello")); //Print True

4. When a Java virtual machine loads a class, it implicitly creates a class instance that describes the class.

Top
1
Step

Methods for creating objects in Java

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.