Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of creating objects in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the methods of creating objects in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1 introduction

Java is an object-oriented programming language, and as long as you use it, you need to create objects. There are six ways for Java to create objects, but there are not so many commonly used ones. Here is the right to record them.

2 six methods

(1) use the new keyword

Pumpkin p1 = new Pumpkin ()

(2) reflected Class class newInstance ()

Pumpkin p2 = Pumpkin.class.newInstance ()

(3) newInstance () of reflected Constructor class

Pumpkin p3 = Pumpkin.class.getDeclaredConstructor () .newInstance ()

(4) clone method of Object object

Pumpkin p4 = (Pumpkin) p1.clone ()

Note that the clone method of the Object class is protected, and when Override, it can be changed to public so that all other classes can call it.

Pay attention to shallow and deep copies.

Here the editor has built a front-end learning and exchange QQ group: 132667127, I have sorted out the latest front-end materials and advanced development tutorials, if you want, you can join the group to learn and communicate together.

(5) deserialization

ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream ("object.bin"))

Oos.writeObject (p1)

Oos.close ()

ObjectInputStream ois = new ObjectInputStream (new FileInputStream ("object.bin"))

Pumpkin P5 = (Pumpkin) ois.readObject ()

Ois.close ()

Must implement the Serializable interface

You need to pay attention to which fields can be serialized, which fields will not be serialized, and how to control

Note the role of serialVersionUID

Understand the differences in Externalizable.

(6) use Unsafe class

Field f = Unsafe.class.getDeclaredField ("theUnsafe")

F.setAccessible (true)

Unsafe unsafe = (Unsafe) f.get (null)

Pumpkin p6 = (Pumpkin) unsafe.allocateInstance (Pumpkin.class)

A method that is rarely used, generally do not need to understand this method.

3 sample code

The sample code is as follows:

Package com.pkslow.basic;import sun.misc.Unsafe

Import java.io.*

Import java.lang.reflect.Field

Import java.lang.reflect.InvocationTargetException

Public class CreateObject {

Public static class Pumpkin implements Cloneable, Serializable {

Public Pumpkin () {

System.out.println ("Constructor called")

}

@ Override

Public Object clone () throws CloneNotSupportedException {

Return super.clone ()

}

}

Public static void main (String [] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, CloneNotSupportedException, IOException, ClassNotFoundException, NoSuchFieldException {

System.out.println ("--start---")

System.out.println ("(1) new")

Pumpkin p1 = new Pumpkin ()

System.out.println ("(2) Class newInstance")

Pumpkin p2 = Pumpkin.class.newInstance ()

System.out.println ("(3) Constructor newInstance")

Pumpkin p3 = Pumpkin.class.getDeclaredConstructor () .newInstance ()

System.out.println ("(4) clone")

Pumpkin p4 = (Pumpkin) p1.clone ()

System.out.println ("(5) Serialization")

ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream ("object.bin"))

Oos.writeObject (p1)

Oos.close ()

ObjectInputStream ois = new ObjectInputStream (new FileInputStream ("object.bin"))

Pumpkin P5 = (Pumpkin) ois.readObject ()

Ois.close ()

System.out.println ("6) Unsafe")

Field f = Unsafe.class.getDeclaredField ("theUnsafe")

F.setAccessible (true)

Unsafe unsafe = (Unsafe) f.get (null)

Pumpkin p6 = (Pumpkin) unsafe.allocateInstance (Pumpkin.class)

System.out.println ("--end---")

}

}

The output is as follows:

-start

(1) new

Constructor called

(2) Class newInstance

Constructor called

(3) Constructor newInstance

Constructor called

(4) clone

(5) Serialization

(6) Unsafe

-end

So the constructor will be executed: the new keyword, two kinds of reflection

The constructors that will not be executed are: clone, serialization, and unloaded classes.

This is the end of the content of "what are the methods of creating objects in Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report