Chapter 9 constructor of JavaSE topic

1. Constructor overview

(1) Introduction to constructor

  • Constructor: it is a special method of class to complete the initialization of new objects;
//Constructor definition method
[Modifier ] Method name(parameter list ){
    Method body;
}

(2) Constructor considerations

① The modifier of the constructor can be default or public, protected or private;
② Constructor has no return value;
③ Method name and class name must be the same;
④ The parameter list has the same rules as the member method;
⑤ The constructor is called by the system – when creating an object, the system will automatically call the constructor of this class to complete the initialization of the object;
⑥ A class can define multiple different constructors, that is, constructor overloading;
⑦ If no constructor is specified during development, the system will automatically generate a default parameterless constructor for the class;
⑧ The default constructor cannot be used unless the constructor is defined by itself;

  • example
public class hello {
    public static void main(String[] args) {
        //When we create an object in new, we specify the name and age directly through the constructor
        Person p0 = new Person();
        Person p1 = new Person("hello");
        System.out.println("p1 The information is as follows:");
        System.out.println("p1 object name=" + p1.name);
        Person p2 = new Person("smith",80);
        System.out.println("p2 The information is as follows:");
        System.out.println("p2 object name=" + p2.name);
        System.out.println("p2 object age=" + p2.age);
    }
}

class Person{
    String name;
    int age;

    //constructor 
    //1. The constructor has no return value and cannot write viod
    //2. The constructor has the same name as the class Person
    //3. (String pName,int pAge) is a list of constructor formal parameters. The rules are the same as member methods
    public Person(){
        System.out.println("The parameterless constructor is called to complete the initialization of the object");
    }

    public Person(String pName){
        System.out.println("A parameter constructor is called to complete the initialization of the object");
        name = pName;
    }

    public Person(String pName,int pAge){
        System.out.println("Two parameter constructors are called to complete the initialization of the object");
        name = pName;
        age = pAge;
    }
}
  • Operation results

2. Constructor memory parsing (object creation)

  • example
public class hello {
    public static void main(String[] args) {
        //When we create an object in new, we specify the name and age directly through the constructor
        Person p2 = new Person("Xiao Qian",80);
        System.out.println("p2 The information is as follows:");
        System.out.println("p2 object name=" + p2.name);
        System.out.println("p2 object age=" + p2.age);
    }
}

class Person{
    String name;
    int age=100;

    public Person(String pName,int pAge){
        System.out.println("Two parameter constructors are called to complete the initialization of the object");
        name = pName;
        age = pAge;
    }
}
  • Operation results

  • Memory parsing

Step 1: load the person class (Person.class) in the method area, which will only be loaded once;
The second step is to allocate space (address) in the heap;
Step 3: initialize the attribute by default (age = 0,name = null);
Step 4 and then explicitly initialize the attribute (age = 90)
Step 5: finally, the constructor initializes the attribute (name = ox1133). Ox1133 is the address of smith in the constant pool;
Step 6: return the heap space address to P, where p is the reference of the object;

3. this keyword

  • Meaning: java virtual opportunity assigns this to each object to represent the current object.

  • Function: the constructor directly uses attributes as formal parameters

  • example

public class hello {
    public static void main(String[] args) {
        //When we create an object in new, we specify the name and age directly through the constructor
        Person p0 = new Person();
        Person p1 = new Person("hello");
        System.out.println("p1 The information is as follows:");
        System.out.println("p1 object name=" + p1.name);
        Person p2 = new Person("smith",80);
        System.out.println("p2 The information is as follows:");
        System.out.println("p2 object name=" + p2.name);
        System.out.println("p2 object age=" + p2.age);
    }
}

class Person{
    String name;
    int age;

    //constructor 
    //1. The constructor has no return value and cannot write viod
    //2. The constructor has the same name as the class Person
    //3. (String pName,int pAge) is a list of constructor formal parameters. The rules are the same as member methods
    public Person(){
        System.out.println("The parameterless constructor is called to complete the initialization of the object");
    }

    public Person(String name){
        System.out.println("A parameter constructor is called to complete the initialization of the object");
        this.name = name;
    }

    public Person(String name,int age){
        System.out.println("Two parameter constructors are called to complete the initialization of the object");
        this.name = name;
        this.age = age;
    }
}
  • Operation results

  • matters needing attention

(1) This keyword can access the properties, methods and constructors of this class;

Ⅰ,Attribute access syntax:this.Attribute name
Ⅱ,Member method access syntax:this.Method name(parameter list)
Ⅲ,Constructor access syntax:this(constructor )

(2) this is used to distinguish the attributes and local variables of the current class;

(3) this cannot be used outside the class definition, but only in the method defined by the class

4. Access modifier

  • Function: control the modification scope of methods and attributes
Serial numberAccess levelAccess modifier similarSame packageSubclassDifferent packages
1openpublic
2under protectionprotected
3defaultNo modifier
4privateprivate
  • Precautions for use
    1. Modifiers can modify attributes, member methods and classes in a class;
    2. Only default and public can modify classes, and follow the above access rights principles;
    3. The access rules of member methods are the same as those of properties

Keywords: Java Back-end

Added by corporateboy on Sat, 12 Feb 2022 02:23:20 +0200