Four ways to create objects in Java

Source: Internet
Author: User
Tags shallow copy

There are four ways to create objects in a Java program:

Call the new statement to create an object, the most common one

Using reflection to create an object, call the Newinstance () instance method of the Java.lang.Class or Java.lang.reflect.Constructor class

Calling the object's clone () method

The ReadObject () method of the Java.io.ObjectInputStream object is called by using serialization means.

The following examples describe

A. New statement creation

    ?// 使用java语言的关键字 new 创建对象,初始化对象数据     ?MyObject mo = new MyObject() ; 

Ii. Clone method Create object

The constructor is not automatically called.PublicClass CreateInstanceImplements cloneable{public CreateInstance getinstance ()Throws clonenotsupportedexception{return (CreateInstance) this. Clone (); If you need to replicate the object instance pointed to by the above obj, call new CreateInstance ().getinstance () method is OK. But why not just use the new CreateInstance () .clone ()? The prototype of the object# Clone () method in the JDK is: protected native Object clone () throws Clonenotsupportedexception; Method modifiers are protected, not public. The invisibility of this access makes us invisible to the Object #clone () method. Therefore, you must override object clone method before you can use it. Java code: public class CreateInstance implements cloneable{public CreateInstance clone throws clonenotsupportedexception{return (CreateInstance) super.clone ();}} It is worth noting that if you need to use the Clone method, you must implement the Java.lang.Cloneable interface, otherwise it throws java.lang.CloneNotSupportedException. In addition, the Clone method does the work of copying the contents of the field directly, in other words, regardless of the object instance content of the field. A field-to-field copy like this is the 

Third, reflection method newinstance Create Object

      利用java.lang.Class类的newInstance方法,则可根据Class对象的实例,建立该Class所表示的类的对象实例。      创建CreateInstace类的对象实例可以使用下面的语句(这样需要一个已经存在的对象实例)。      CreateInstance instance = CreateInstance.class.newInstance();      或者使用下面的语句(只需要存在相应的.class文件即可) CreateInstance instance = (CreateInstance)Class.forname("com.create.instance.CreateInstance").newInstance(); 如果包下不存在相应.class文件,则会抛出ClassNotFoundException。 注意 :newInstance创建对象实例的时候会调用无参的构造函数,所以必需确保类中有无参数的构造函数,否则将会抛出java.lang.InstantiationException异常。 无法进行实例化。

Iv. serializing the ReadObject () method to create an object

?

如果对象是通过ObjectInputStream类的readObject()方法创建的,那么Java虚拟机通过从输入流中读入的序列化数据来初始化那些非暂时性(non-transient)的实例变量。在其他情况下,如果实例变量在声明时被显式初始化,那么就把初始化值赋给实例变量,接着再执行构造方法。这是最常见的初始化对象的方式。

Four ways to create objects in Java

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.