Is the constructor object created in Java?

Source: Internet
Author: User

First, it is completely wrong to say that the phrase "constructor creation object in Java" is a complete error.

The constructor in Java is primarily intended to initialize the value of a variable ... In fact, before executing the constructor, the memory space required by the Java object has been generated ...

Can generally be understood as created by the New keyword OH.

At some point, after the object is created by the new keyword, the value of the corresponding variable can be initialized by the corresponding construction method.

But in some special cases, we can create related objects without using the New keyword.

Two common ways to create objects without using the New keyword are as follows:

1) Create related objects through the serialization and deserialization of Java ...

2) Create related objects through the clone of Java ...

These two ways of creating objects are explained below:

1) Creating Java objects in a Java serialization way

Specific examples are as follows:

Package Com.yonyou.test;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.objectinputstream;import Java.io.objectoutputstream;import java.io.serializable;/** * Test class * @author Small Hao * @ Creation date 2015-3-2 */public class test{Publ  IC static void Main (string[] args) throws FileNotFoundException, IOException, classnotfoundexception {Wolf wolf_old=new   Wolf ();   Wolf Wolf_new=null;   Create an object output stream ObjectOutputStream outstream=new objectoutputstream (New FileOutputStream ("A.txt"));   ObjectInputStream inputstream=new ObjectInputStream (New FileInputStream ("A.txt"));   Outstream.writeobject (Wolf_old);   wolf_new= (Wolf) inputstream.readobject ();   System.out.println (Wolf.count); System.out.println ("Wolf_old and wolf_new equal?"  "+ (wolf_old==wolf_new)); }}/** * Create wolf this test object * @author Small Hao * @ Created date 2015-3-19 */class Wolf implements serializable{private static final long Serialv Ersionuid = 1l;static int count=100;public WolF () {count-=10; System.out.println ("Hello, we are doing the construction method ..."); System.out.println (count);}}

It is important to note that when using Java serialization and deserialization, the corresponding entity classes are implemented serializable serialization interface Oh ...

It is also important to note that the process of deserializing through an IO stream creates objects that are exactly the same content as the original objects, but they are different objects ...

Objects created at the same time by serialization and deserialization do not invoke the original object's construction method ...

It also reminds us that we can actually use the "private construction method to implement a singleton mode" which is probably unsafe (for what?). )...

If you want to not produce multiple instances of Java objects during deserialization, you should provide a readresolve () method for the Singleton class that guarantees

Get an existing Java instance in the process of deserialization ...

Specific examples are as follows:

     

Package Com.yonyou.test;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.objectinputstream;import Java.io.objectoutputstream;import java.io.serializable;/** * Test class * @author Small Hao * @ Creation date 2015-3-2 */public class test{Publ IC static void Main (string[] args) throws FileNotFoundException, IOException, classnotfoundexception {Wolf Wolf_old=wol   F.getwolf ();   Wolf Wolf_new=null;   Create an object output stream ObjectOutputStream outstream=new objectoutputstream (New FileOutputStream ("A.txt"));   ObjectInputStream inputstream=new ObjectInputStream (New FileInputStream ("A.txt"));   Outstream.writeobject (Wolf_old);   wolf_new= (Wolf) inputstream.readobject ();   System.out.println (Wolf.count); System.out.println ("Wolf_old and wolf_new equal?"  "+ (wolf_old==wolf_new)); }}/** * Create wolf this test object * @author Small Hao * @ Created date 2015-3-19 */class Wolf implements serializable{private static final long Serialv Ersionuid = 1L; static int Count=100;statiC Wolf Wolf=null;private Wolf () {count-=10; System.out.println ("Hello, we are doing the construction method ..."); System.out.println (count);} public static Wolf Getwolf () {if (wolf==null) {wolf=new wolf ();} return wolf; /** * Method constructed for implementing a single instance in the deserialization process */private Object readresolve () {return wolf;}

2) Create related objects through the clone of Java ...

The preferred cloned object needs to implement the cloning interface: clonable

Next, the cloned object needs to override the Clone method

Specific examples are as follows:

Package Com.yonyou.test;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.objectinputstream;import Java.io.objectoutputstream;import java.io.serializable;/** * Test class * @author Small Hao * @ Creation date 2015-3-2 */public class test{Publ  IC static void Main (string[] args) throws FileNotFoundException, IOException, classnotfoundexception {Wolf wolf_old=new   Wolf ();   Clones the corresponding object and assigns it to the new object Wolf wolf_new= (Wolf) Wolf_old.clone ();   System.out.println (Wolf.count); System.out.println ("Wolf_old and wolf_new equal?"  "+ (wolf_old==wolf_new)); }}/** * Create wolf this test object * @author Small Hao * @ Created date 2015-3-19 */class Wolf implements cloneable{static int count=100;static Wolf WOL F=null;public Wolf () {count-=10; System.out.println ("Hello, we are doing the construction method ..."); System.out.println (count);} /** * The method to be overridden when implementing the cloning interface, but not required */public Object clone () {Wolf wolf=null;try {wolf= (Wolf) Super.clone ();} catch ( Clonenotsupportedexception e) {e.printstacktrace ();} Return WOLf;}} 

Similarly, the cloned two objects are two different objects oh ...

All right, let's get here today.

  

Is the constructor object created 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.