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
- Public class Customer implements cloneable{
- private String name;
- private int age;
- Public Customer () {
- This ("Unknown",0);
- System.out.println ("call default constructor");
- }
- Public Customer (String name,int age) {
- This.name=name;
- This.age=age;
- System.out.println ("call second constructor");
- }
- Public Object Clone ()throws clonenotsupportedexception{
- return Super.clone ();
- }
- Public Boolean equals (Object o) {
- if (this==o)return true;
- if (! (o instanceof Customer)) return false;
- Final Customer other= (customer) O;
- if (this.name.equals (other.name) && this.age==other.age)
- return true;
- Else
- return false;
- }
- Public String toString () {
- return "name=" +name+", age=" +age;
- }
- Public static void Main (String args[])throws exception{
- Using reflection to create a customer object
- Class objclass=class.forname ("Customer");
- Customer c1= (Customer) objclass.newinstance (); //Invokes the default construction method of the Customer class
- System.out.println ("C1:" +C1); //Print name=unknown,age=0
- Create a customer object with the new statement
- Customer c2=New Customer ("Tom",20);
- System.out.println ("C2:" +C2); //Print name=tom,age=20
- Creating a Customer object using cloning
- Customer c3= (Customer) C2.clone (); //Does not invoke the construction method of the Customer class
- System.out.println ("C2==C3:" + (C2==C3)); //Print false
- System.out.println ("C2.equals (C3):" +c2.equals (C3)); //Print True
- System.out.println ("C3:" +C3); //Print name=tom,age=20
- }
- }
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
- String s1="Hello";
- String s2="Hello"; //s2 and S1 reference the same string object
- String s3=new String ("Hello");
- System.out.println (S1==S2); //Print True
- 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
- String s1="H";
- String s2="Ello";
- String s3=s1+s2; //S3 refers to a new string object
- System.out.println (s3=="Hello"); //Print false
- 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