How to create and construct Java objects

Source: Internet
Author: User

first, four ways to create objects:

A. New statement;

B. Use reflection to invoke the Newinstance () instance method of the class object describing the classes;

C. Clone of the calling object ();

D. deserialization;

where new and newinstance () call the constructor method of the class, and clone () and deserialization do not;

Cloneable interface:

There is no method defined in the cloneable interface, only for tagged objects, and the clone () method is the method inside the object class, which is a protected native method;

If the object implements the Cloneable interface, it needs to overwrite the clone method;

  Shallow copy : copies only basic types;

 public classPersonImplementscloneable{Private intage ; PrivateString name;  publicPerson (intage , String Name) {           this. Age =age ;  this. Name =name; }             publicperson () {} public intgetage () {returnage ; }         publicString getName () {returnname; } @OverrideprotectedObject Clone ()throwsclonenotsupportedexception {return(person)Super. Clone (); }  }  
View Code

  deep copy : Both the base type and the reference type are copied;

Static classBodyImplementscloneable{ publicHead head;  publicBody () {} publicBody (head Head) { this. Head =head;} @OverrideprotectedObject Clone ()throwsclonenotsupportedexception {return Super. Clone (); }        }  Static classHead/*implements Cloneable*/{       publicFace face ;  publicHead () {} publicHead (face Face) { this. face =face ;} }    public Static voidMain (string[] Args)throwsclonenotsupportedexception {body Body=NewBody (NewHead ()); Body body1=(Body) Body.clone (); System.out.println ("body = = body1:" + (body = =body1)); System.out.println ("body.head = = body1.head:" + (body.head = =body1.head)); }
View Code

Refer To: clone and new which is faster

  

serializable interface Serialization :

The string class, the wrapper class, the date class all implement this interface;

  to Serialize a write to an object:

New    ObjectOutputStream (new// Create output stream out.writeobject ("hello"); Out.writeobject (new// write to Object

  to deserialize a read object:

New ObjectInputStream (new// Create input stream String str =// Read Object

Further control:

Defines the ReadObject () and WriteObject () methods, which are executed when ObjectOutputStream serializes an object, if the object has a writeobject () method;

    Private void throws ioexception{        stream.defaultwriteobject ();        Stream.writeobject (new  Object ());    }         Private void throws classnotfoundexception, ioexception {            stream.defaultreadobject ();             = Stream.readobject ();    }

Reference to: Java Serialization Basics serializable and Externalizable differences

The default constructor is not called when the serializable is serialized, and the default constructor is called when the externalizable is serialized;

    

second, the construction method

Each class has a default constructor method that does not contain parameters;

If you customize a constructor method, the default construction method disappears;

When a subclass creates an object, it automatically calls the default constructor of the parent class, and the subclass can also display a constructor that uses super () to call the parent class (so defining a class will show the definition of an empty construction method to prevent it from missing a subclass Call)

How to create and construct Java objects

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.