Java has five main ways of creating objects

1. Use the new keyword (most commonly used) :

objectname obj = new objectname();

2. Newinstance () using the reflection class:

objectname obj = objectname.class.newinstance();

3. Use the constructor class newinstance() method:

objectname obj = objectname.class.getconstructor.newinstance();

Clone ()

objectname obj = obj.clone();

5. Use objectinputStream’s ReadObject () method:

objectname obj = new objectinputstream(new fileinputstream(file_name)).readobject();

Code examples:

1. Create a user class:

1 package com.example.demo.model; 2 3 import java.io.serializable; 4 import java.util.objects; 5 6 public class user implements serializable, cloneable { 7 private static final long serialversionuid = 1l; 8 private string id; 9 private string name; 10 private string phone; 11 12 public user(string id, string name, string phone) { 13 this.id = id; 14 this.name = name; 15 this.phone = phone; 16 } 17 18 public user() { 19 } 20 21 public string getid() { 22 return id; 23 } 24 25 public void setid(string id) { 26 this.id = id; 27 } 28 29 public string getname() { 30 return name; 31 } 32 33 public void setname(string name) { 34 this.name = name; 35 } 36 37 public string getphone() { 38 return phone; 39 } 40 41 public void setphone(string phone) { 42 this.phone = phone; 43 } 44 45 @override 46 public user clone() throws clonenotsupportedexception { 47 return (user) super.clone(); 48 } 49 50 @override 51 public boolean equals(object o) { 52 if (this == o) return true; 53 if (o == null || getclass() ! = o.getclass()) return false; 54 user user = (user) o; 55 return objects.equals(id, user.id) && 56 objects.equals(name, user.name) && 57 objects.equals(phone, user.phone); 58 } 59 60 @override 61 public int hashcode() { 62 return objects.hash(id, name, phone); 63 } 64 65 @override 66 public string tostring() { 67 return "user{" + 68 "id='" + id + '\'' + 69 ", name='" + name + '\'' + 70 ", phone='" + phone + '\'' + 71 '}'; 72 } 73 } javaCopy the code

2. Then start creating the user object:


1 package com.example.demo.practice;

2

3 import com.example.demo.model.user;

4

5 import java.io.fileinputstream;

6 import java.io.fileoutputstream;

7 import java.io.objectinputstream;

8 import java.io.objectoutputstream;

9

10 public class objectcreation {
11 private static final string file_name = "user.obj";

12

13 public static void main(string[] args) throws exception {
14 // Use the new keyword in method one

15 user user = new user("1"."Zhang"."135 * * * * 8457");

16 system.out.println(user.tostring());

17

18 // Use the class newinstance() method

19 user user2 = user.class.newinstance();

20 user2.setname("Bill");

21 system.out.println(user2.tostring());

22

23 // Use the constructor class newinstance() method

24 user user3 = user.class.getconstructor().newinstance();

25 user3.setname("Fifty");

26 system.out.println(user3.tostring());

27

28 // Use the clone() method, provided that the clone class implements the cloneable interface and overrides its clone() method

29 user user4 = user.clone();

30 system.out.println(user4.tostring());

31 system.out.println(user == user4);

32 system.out.println(user.equals(user4));

33

34 5. Call objectinputStream's ReadObject () method using deserialization if the class implements serialIZABLE interface

35 / / the serialization

36 objectoutputstream oos = new objectoutputstream(new fileoutputstream(file_name));

37 oos.writeobject(user);

38 // deserialize

39 objectinputstream ois = new objectinputstream(new fileinputstream(file_name));

40 user user5 = (user) ois.readobject();

41 system.out.println(user5.tostring());

42 }

43 }
Copy the code

The following output is displayed:

user{id='1', name='Joe', phone='* * * 135 * 8457'}

user{id='null', name='bill', phone='null'}

user{id='null', name='Cathy', phone='null'}

user{id='1', name='Joe', phone='* * * 135 * 8457'}

false

true

user{id='1', name='Joe', phone='* * * 135 * 8457'}

Copy the code