search
HomeJavaJavagetting StartedWhat are the ways to create objects in Java

What are the ways to create objects in Java

Jul 15, 2020 pm 04:51 PM
javaobject

What are the ways to create objects in Java

Several ways to create objects:

(Recommended tutorial: java introductory tutorial)

1. This is the most Commonly used methods: create objects through new. This method requires the use of a constructor.

Demo demo1=new Demo();
Demo demo2=new Demo(1,"有参构造器");

2. The more commonly used method: create objects through reflection newInstance(). This method uses constructors.

Demo demo2=(Demo) Class.forName("Demo").newInstance();

3. Through the clone method of the Object class. You need to implement the Cloneable interface and override the clone method of the object class.

Whenever we call the clone method of an object, the jvm will create a new object and copy all the contents of the previous object into it. Creating an object using the clone method does not call any constructor.

Demo demo4=(Demo) demo2.clone();

(Video tutorial recommendation: java video tutorial)

4. Deserialization

JSON data and Java objects are often processed in java conversion, namely serialization and deserialization.

When we serialize and deserialize an object, the JVM will create a separate object for us. During deserialization, the JVM creates the object and does not call any constructor. In order to deserialize an object, we need to make our class implement the Serializable interface, although this interface does not have any methods. No constructors will be called.

The above is the detailed content of What are the ways to create objects in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools