Summary of methods in Java that do not create objects by constructing methods

Source: Internet
Author: User

We often say that Java is an object-oriented language, so almost all operations in Java are inseparable from objects. In the Java language, the most common way to create objects is through calls to the class constructor, but there are several other ways to create objects.

1) Create objects by reflection mechanism;

classperson{String name= "Jack";  PublicPerson () {System.out.println ("Construct"); }     PublicString toString () {returnname;} } Public  classtest{ Public Static voidMain (string[] args) {Class ClassType; Try{ClassType=class.forname ("Person"); Person P=(person) classtype.newinstance ();            SYSTEM.OUT.PRINTLN (P);            }cathch (Exception e) {e.printstacktrace (); }     }  }

The running result of the program is:

Construct

Jack

2) Calling the object's Clone method requires the following steps to use the Clone method:

(1) The class that implements clone first needs to inherit the Cloneable interface is essentially an identity interface, and there is no interface method, which is similar to the serialization interface Serializable ().

(2) Override the Clone method of the object class in the class.

(3) Call Super.clone () in the Clone method. Regardless of the inheritance structure of the Clone class, Super.clone () invokes the Clone () method directly or indirectly in the Java.long.Object class.

The instance code is as follows:

classOBJ Implement cloneable{Private intAint=0;  PublicObj () {System.out.println ("Construct"); }      Public intGetaint () {returnAInt;}  Public voidChangeint () { This. aint=1; }      PublicObject Clone () {object o=NULL; Try{o= (OBJ)Super. Clone (); }Catch(clonenotsuppertedexception e) {e.printstacktrace (); }       return0; }     }      Public classtest{ Public Static voidMain (string[] args) {OBJ a=NewOBJ (); OBJ b=(OBJ) A.clone ();                B.changeint (); System.out.println ("A:" +a.getaint ()); System.out.println ("B:" +b.getaint ()); }     }

The running result of the program is:

Construct

a:0

B:1

As you can see from the program running above, when the A.clone () method is called, the system creates a new object, but does not call the constructor method.

3) Create the object by deserializing it in the following instance code:

       ImportJava.io.FileInputStream; ImportJava.io.FileOutputStream; ImportJava.io.ObjectInputStream; ImportJava.io.ObjectOutputStream; Importjava.io.Serializable;  Public classPerson implement serilalizable{PrivateString name;  PublicPerson () { This. Name= "Lili"; System.out.println ("Construct"); }               PublicStream toString () {return  This. Name;  Public Static voidMain (String args[]) {person P=Newpeople ();                            SYSTEM.OUT.PRINTLN (P); ObjectOutputStream Oos=NULL; ObjectInputStream Ois=NULL; Try{FileOutputStream fos=NewFileOutputStream ("Perpke.out"); Oos=NewObjectOutputStream (FOS);                              Oos.writeobject (P); Oos.close (0); } Catch(Exception ex) {} people pl; Try{FileInputStream fis=NewFileInputStream ("Perple.out"); Ois=NewObjectInputStream (FIS); P1=(People) ois.readobject ();                                 SYSTEM.OUT.PRINTLN (P); if(p!=p1) System.out.println ("Different Objecrt")Ois.close (); }Catch(Exception ex) {} }}

The running result of the program is:

Construct

Lili

Lili

Fifferent Object

Summary of methods in Java that do not create objects by constructing methods

Related Article

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.