Learning notes -- Java construction method

Posted by twinedev on Thu, 03 Feb 2022 06:19:16 +0100

Java construction method

About construction method

  • The construction method is also called Constructor / Constructor / Constructor

  • Syntax structure:

      [Modifier list] Construction method name(Formal parameter list){
          Construction method body;
      }
    

    Compare the grammatical structure of common methods

      [Modifier list] Return value type constructor name(Formal parameter list){
          Construction method body;
      }
    
    • For construction methods, the return value type does not need to be specified, and there is no void

    • Writing void is a common method

  • The method name of the construction method must be consistent with the class name

  • Function of construction method:

    • Existential meaning. Objects can be created by calling construction methods
    • While creating the object, initialize the memory space of the instance variable
  • How to call the constructor?

    Normal method call:

    • static: class name Method name (argument list)
    • No static: reference Method name (argument list)

    Constructor call:

    • new construction method name (argument list)
  • Constructor has return value

    But you don't need to write the return value;, After the construction method is completed, the Java program returns automatically

    Since the return value type is the type of the class where the constructor is located, the return value type does not need to be written

    If there is no artificial write constructor in the class, there is also a default parameterless constructor:

      new Construction method name(){
          // There is code to initialize the memory space of the instance variable
          // Otherwise, where does the default value of the instance variable come from
      }
    

    [recall] default value of instance variable:

    • byte,short,int,long default 0
    • float,double default 0.0
    • The default value of boolean is false
    • Reference data type default value null
  • The construction method supports overloading mechanism

public class ConstructorTest {
    
    public static void main(String[] args) {
        
        User u1 = new User();
        User u2 = new User("jock");
        User u3 = new User(10);
    }
    
}
public class User{

    // User number
    private int id;

    // User name
    private String name;


    // The following three construction methods,
    // this can be temporarily regarded as a substitute for the object or instance after the class reference
    public User(){
    }

    public User(String n){
        this.name = n;
        System.out.println("String");
    }

    public User(int num){
        this.id = num;
        System.out.println("int");
    }
}

Parameter transfer

  • [review] sort out objects and References:

    • Object [instance]: the memory space opened in heap memory by using new operator is called object

    • Reference: it is a variable, not necessarily a local variable, but also a member variable.

      The reference holds the memory address and points to the object in the heap memory

Variation of variables in parameter transfer

  • unchanged
public class Test01 {
    
    public static void main(String[] args) {

        int a = 10; // 10 A is the local variable of the main method

        add(i);     // 11 Where the value of main method a is passed into add method i
        // Use add(i); i + + in does not affect the main method a

        System.out.println(a);  // 10 no change
    }

    public static void add(int i){

        // i is the local variable of the add method
        i++;
        System.out.println("add --> " + i);
    }
}
  • change
public class Test02 {
    
    public static void main(String[] args) {
        
        User u = new User(10);  // u.age    10

        add(u); // 11
        // In u is the memory address, pointing to the object
        // After u is passed in, the user will also save the same memory address as that in U
        

        System.out.println("main --> " + u.age);    // 11
        // Although it is two addresses, it points to the same object
    }

    public static void add(User user){ 

        user.age++;
        System.out.println("add --> " + user.age);  
    }
}

class User{

    // Instance variable
    int age;

    // Construction method
    public User(int i){
        age = i;
    }
}

Metaphor:

  • Both of them have 10 yuan in cash. Although they have the same amount of money, they are different. Yes, but it's 20 yuan, each for its own use

  • Both of them know a string of passwords [bank cards share 10 yuan] and can withdraw and deposit money in the bank through the password. Yes, it's 10 yuan. It's shared through the password

    • Password ---- the memory address saved in the reference
    • Bank card - object [example]
    • Go to the bank - Method
    • Money - instance variable

General data

int i = 10;
int j = i;  
// When i is passed to j, it actually just passes the 10 saved in the i variable to J. J is actually a new memory space

Reference data involved

User u1 = 0x1234;   // 0x1234 memory address, new operator + call construction method
User u2 = u1;   
// When u1 is passed to u2, it actually assigns the value 0x1234 to u2. As above, u1 and u2 are two different local variables

// However, on top of this, u1 and u2 save memory addresses 0x1234, pointing to the same object [instance] in heap memory

Topics: Java