In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about what is a constructor in the Java language. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
What is a construction method?
Construction method with parameters
1.3.1 What is a construction method?
In Java language, when creating an object, you need to do the corresponding initialization work for the created object, such as assigning memory addresses to the attributes of the object and setting the initial values of the attributes of the object. This is done by a special method in Java called a constructor.
In every Java class, a constructor is provided. If we do not define constructors in this class, then the compiler automatically calls implicit constructors in this class.
Syntax rules for constructing methods:
Method name must be the same as class name
2. There is no return type, and void cannot be written.
Example 7 gives an example of a constructor in which the object attributes name and age are given initial values.
Example 7
package org.hopetech.l01;
public class Employee4 {
private String name; //employee name
private int age; //employee age
public Employee4(){
this.name = "Luo Zhiguo";
this.age = 30;
}
/**
* Employees introduce themselves
*/
public void selfIntroduction() {
System.out.println("I am" + name + ", I have" + age + "this year! ");
}
}
Objects may not call constructor methods, which are specifically used to create objects. Therefore constructor methods must be called by the new keyword. Employee4 employee = new Employee4();
Example 8
package org.hopetech.l01;
public class Employee3Test {
public static void main(String[] args) {
//call constructor to instantiate employee class
Employee4 employee = new Employee4();
//invoke employee self-introduction method
employee.selfIntroduction();
}
}
When instantiating the object, we have already initialized the object properties by assigning default values to the instance object properties through the constructor.
If we want to explicitly assign initial values to instance object properties, that is, we instantiate the class into multiple objects, and the property values of each object are not different. At this point, we need to use construction methods with parameters.
1.3.2 Construction methods with parameters
Constructors with parameters can initialize the properties of an object by passing parameters through the constructor when instantiating the object.
Example 9
package org.hopetech.l01;
public class Employee5 {
private String name; //employee name
private int age; //employee age
public Employee5(String name,int age){
this.name = name;
this.age = age;
}
/**
* Employees introduce themselves
*/
public void selfIntroduction() {
System.out.println("I am" + name + ", I have" + age + "this year! ");
}
}
Obviously, we explicitly assign initial values to individual instance properties of the Employee5 employee class through the parameterized constructor method.
When instantiating an object using a parameterized constructor, the values passed must match exactly the constructor's parameters in number, order, and data type.
example 10
package org.hopetech.l01;
public class Employee5Test {
public static void main(String[] args) {
//instantiate an employee using parameter-based construction methods
Employee5 employee1 = new Employee5("Li Dong",24);
Employee5 employee2 = new Employee5("Luo Zhiguo",30);
//invoke employee self-introduction method
employee1.selfIntroduction();
employee2.selfIntroduction();
}
}
By invoking the construction method with parameters, the initialization of object attributes is completed at the same time of object creation, and the operation of object initialization is simplified.
We may have different initialization behaviors for classes in different situations. So we might have to write multiple constructors. What should we do about that? We will learn an important concept below: method overloading.
Thank you for reading! About "What is a construction method in Java language" This article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!
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.