In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how Java creates objects, Xiaobian feels quite practical, so share it with you as a reference, I hope you can gain something after reading this article.
Explicitly Create Objects
There are four ways to explicitly create objects.
1. Create objects using the new keyword
This is a common way to create objects, and the syntax is as follows:
class name object name = new class name ();
2. Call java.lang.Class
or newlnstance() instance method of java.lang.reflect. Conductor class
In Java, objects can be created using java.lang.Class or newlnstance() instance methods of java.lang.reflect. Conductor class, with the following code format:
java. lang. ClassObjectName = java.lang.Class.forName(full name of class to instantiate); ClassObjectName = (ClassName) ClassObjectName.newInstance();
When calling the forName() method in the java.lang.Class class, you need to pass the full name of the class to be instantiated (such as com.mxl.package.Student) as an argument, and then call the newInstance() method of the java.lang.Class object to create the object.
3. Call the clone() method of the object
This method is not commonly used, and when you create objects using it, the class you want to instantiate must inherit the java.lang.Cloneable interface. The syntax for calling the clone() method of an object to create an object is as follows:
class name object name = (class name) created class object name.clone();
4. Call the readObject() method of the java.io.ObjectlnputStream object
Example 1:
Let's create an example that demonstrates the first three common methods of object creation. The sample code is as follows:
public class Student implements Cloneable { //implement Cloneable interface private String Name; //student name private int age; //Student age public Student(String name,int age) { //construction method this.Name = name; this.age = age; } public Student() { this.Name = "name"; this.age = 0; } public String toString() { return"Student Name: "+Name+", Age: "+age "; } public static void main(String[] args)throws Exception { System.out.println("---------"); //Create an object using the new keyword Student1 = new Student("Xiao Liu",22); System.out.println(student1); System.out.println("----------- //call newInstance() method of java.lang.Class to create object Class c1 = Class.forName("Student"); Student student2 = (Student)c1.newInstance(); System.out.println(student2); System.out.println("------------------- //call clone() method of object to create object Student student3 = (Student)student2.clone(); System.out.println(student3); }}
An illustration of the above example is as follows:
When an object is created using the new keyword or the newInstance() method of a Class object, the constructor method of the class is called.
When you create an object using the newInstance() method of the Class, the default constructor of the class, the parameterless constructor, is called.
When you create an object using the clone() method of the Object class, instead of calling the class constructor method, it creates a duplicate object with a different memory address than the original object, but with the same attribute values.
If the class does not implement the Cloneable interface, clone. Method throws a java.lang.CloneNotSupportedException exception, so you should have your class implement the Cloneable interface.
The results of the procedure are as follows:
---------
Student Name: Xiao Liu, Age: 22
------------Call newInstance() method of java.lang.Class to create object---------
Student name: name, age: 0
-------------------
Student name: name, age: 0
II. Implicit creation of objects
In addition to explicitly creating objects, you can also create objects implicitly in Java programs, such as the following.
1) String strName = "strValue", where "strValue" is a String object implicitly created by the Java Virtual Machine.
2) The result of the "+" operator operation on a string is a new String object, as shown in the following example:
String str1 = "Hello";String str2 = "Java";String str3 = str1+str2; // str3 references a new String object
3) When the Java Virtual Machine loads a class, it implicitly creates a Class instance describing the class.
Tip: Class loading refers to reading the binary data in the class's.class file into memory, storing it in the method area of the runtime data area, and then creating a java.lang.Class object in the heap area to encapsulate the class's data structure in the method area.
Regardless of the method used to create an object, the Java Virtual Machine includes the following steps when creating an object:
Allocate memory to objects.
Automatically initializes an object's instance variables to default values for their variable types.
Initialize objects, giving instance variables the correct initial values.
Note: Each object is independent of each other, occupies an independent memory address in memory, and each object has its own life cycle. When the life cycle of an object ends, the object becomes garbage, which is handled by the garbage collection mechanism of Java Virtual Machine.
About "Java how to create objects" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.