Constructor, also called constructor method and constructor function, is a special type of method responsible for initializing member variables (fields) in a class. The purpose of the constructor is to perform initialization when creating an object. When an object is created, the system will perform default initialization for the instance of the object.
What is a constructor
1. Constructor, also called constructor method and constructor function. Its function is to construct an instance of a class and ensure that the object is initialized.
2. Constructor format: Permission modifier class name (no parameters/with parameters){}.
3. Depending on whether there are parameters, it can be divided into parameterless construction and parameterized construction.
The biggest use of the constructor is to perform initialization when creating an object. When an object is created, the system will perform default initialization for the instance of the object. If you want to change this default initialization, you can do it through a custom constructor.
Constructors can be used to initialize data members when initializing objects. A class can have multiple constructors. The name of a class's constructor must match the name of the class. To exit the construction, you can use the return statement "return;"
Characteristics of the constructor
1. Different from the general method name, the constructor method name must be the same as the class The name remains consistent, and there is no return value, not even void.
2. The Java compiler will automatically create a parameterless constructor, so even if there is no parameterless constructor in the class, we can omit it. There is no need to assign a value when instantiating an object
3. If a parameterized constructor already exists in the class, the compiler will no longer provide a default parameterless constructor. A value needs to be assigned when instantiating an object, otherwise an error will be reported.
4. When a class instantiates an object, the constructor is automatically called.
5. Cannot be modified by static, final, synchronized, abstract and native. Constructors cannot be inherited by subclasses
6. Each class can have zero or more constructors.
Example 1: No-parameter construction
Student Class
public class Student { //权限修饰符是public,表示内部属性能被其他类访问到。若使用private,则需要set/get才能访问 public String name; public int age; //无参构造,编译器自带,可不写。 public Student(){ System.out.println("括号内无参数,这就是无参构造"); } }
Test Class
public class Test { public static void main(String[] args) { Student st = new Student();//创建对象,括号内无需赋值 //没赋值的情况下,字符类型默认值为null,数值类型默认值为0 System.out.println(st.name+" "+st.age);//null,0 //对属性进行赋值 st.name="Tom猫"; st.age=5; System.out.println(st.name+"今年"+st.age+"岁了"); } }
Result
括号内无参数,这就是无参构造 null 0 Tom猫今年5岁了
Example 2: Parameterized Construction
Student Class
public class Student { //权限修饰符是public,表示内部属性能被其他类访问到。若使用private,则需要set/get才能访问 public String name; public int age; //无参构造最好也写上 public Student(){ } public Student(String name,int age){ System.out.println("括号内有参数,就是有参构造"); //将形参变量的值,赋给成员变量。 this.name = name; this.age = age; //this.name = "吴邪" 若直接在构造方法内部赋值,则优先执行其内部的值,即吴邪会替掉Tom猫 } }
Test class
public class Test { public static void main(String[] args) { //创建对象。赋值后,定义的值会传递给构造器中的形参变量。 Student st = new Student("Tom猫",5);//创建对象,须在括号内赋值,不然报错 //使用对象调用成员变量(java对象只能调用成员变量) System.out.println(st.name+"今年 "+st.age+"岁了"); } }
Result
括号内有参数,就是有参构造 Tom猫今年5岁了
The above is the detailed content of What is a java constructor?. For more information, please follow other related articles on the PHP Chinese website!