How Java objects are instantiated

Source: Internet
Author: User
Tags object serialization serialization

Java objects are instantiated in the following ways:
1. Use new
2. Factory mode
3. Reflection
4. Clone () method
5. Deserialization mode

/**

    • implementing Cloneable and Serializable interfaces
    • */
      public class book implements Cloneable, Serializable {
      Private static final long serialversionuid = 1L;

      Private Integer Serialnum;//serial number
      private String name;//Library name

      Public book () {
      System.out.println ("default constructor ");
      }

      Public book (Integer serialnum, String name) {
      System.out.println ("with parameter constructor");
      This.serialnum = Serialnum;
      THIS.name = name;
      }

      Public int getserialnum () {
      return serialnum;
      }
      public void Setserialnum (int serialnum) {
      This.serialnum = serialnum;
      }
      Public String GetName () {
      return name;
      }
      public void SetName (String name) {
      THIS.name = name;
      }

      @Override
      Public String toString () {
      if (name = = null) {
      Return "book properties not yet filled in";
      }

        return serialnum + ":" + name;  

      }

      @Override
      Protected Object Clone () throws Clonenotsupportedexception {
      return super.clone ();
      }
      }

Class Bookfactory {
public static book getinstance (Integer serialnum, String name) {
return new book (serialnum, name);
}
}

public class Instantiation {

1. Instantiate the object through new public static book Newmode () {System.out.println ("==================================================    ==========");    System.out.println ("Instantiate object with new");    Book book = new book (1, "The Boulevard Sails"); return book;}    2. The object is instantiated by the factory, but it is still new, but the details of the instantiation are given to the factory for processing, and the functions that are not related to the business code are shielded to a certain extent, and the business code is decoupled public static book Factorymode () {    System.out.println ("============================================================");    System.out.println ("Instantiate objects by way of factory"); Return Bookfactory.getinstance (2, "The Boulevard Sails"); 3. Instantiate the object by Reflection @suppresswarnings ("Rawtypes") public static book Reflectmode () throws Exception {System.out.println ("=    ===========================================================");    System.out.println ("Instantiating objects by reflection");    class[] parametertypes = new class[] {integer.class, string.class};    Constructor Constructor = Book.class.getConstructor (parametertypes);    Book book = (book) constructor.newinstance (3, "the Avenue Sails"); return book;} 3-1. There is another way to instantiate an object by reflection, but it is essentially the same as above, but requires the class itself to have a parameterless constructor public Static book Reflectanothermode () throws Exception {System.out.println ("==============================================    ==============");    System.out.println ("Another way to instantiate an object by reflection"); Book book = (book) Class.forName ("instantiation.    Book "). newinstance (); return book;} 4. Invoke the Clone () method of an existing object to instantiate the object public static book Clonemode throws Exception {System.out.println ("=============    ===============================================");    System.out.println ("invokes the Clone () method instantiation object of an existing object");    Book Newbook = (book) book.clone (); System.out.println ("The original object and the cloned object are the same object?")    "+ (book = = Newbook)); return Newbook;} 5. Object serialization can be created by deserializing the object public static book Unserializedmode () throws Exception {System.out.println ("==================    ==========================================");    System.out.println ("Object can be deserialized to create objects after serialization");    Book book = new book (5, "The Boulevard Sails");    ObjectOutputStream ObjectOutputStream = new ObjectOutputStream (new FileOutputStream (New File ("D:/book.obj")); Objectoutputstream.wrIteobject (book);    ObjectInputStream ObjectInputStream = new ObjectInputStream (new FileInputStream (New File ("D:/book.obj"));    Book Newbook = (book) objectinputstream.readobject (); System.out.println ("The original object and the deserialized object are the same?")    "+ (book = = Newbook)); return Newbook;}    public static void Main (string[] args) throws Exception {System.out.println (Instantiation.newmode ());    System.out.println (Instantiation.factorymode ());    System.out.println (Instantiation.reflectmode ());    System.out.println (Instantiation.reflectanothermode ());    Book book = new book (4, "The Boulevard Sails");    System.out.println (Instantiation.clonemode (book)); System.out.println (Instantiation.unserializedmode ());}

}

See the wonderful fantasy world, in the "Road Sail" https://book.qidian.com/info/1012993779

How Java objects are instantiated

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.