Detailed constructor for java

Posted by locell on Tue, 07 Dec 2021 19:47:52 +0100

Constructor Details

1. What is a constructor

  • Constructors in classes, also known as construction methods, must be called when creating objects, and they have the following two characteristics.
    • Must have the same name as the class
    • There must be no return type and no void can be written.
  1. We also create two documents named person and application.

Let's simply create a class

public class person {

}

Another file uses new to call this person.

public class application {
    public static void main(String[] args) {
//An object is instantiated using the new keyword.
        person person = new person();
    }
}

Then run the second file and find that it still works, but what we learned before was that in the first file person, there must be one method to be new in the other file. However, in the first file I wrote, there is no writing method, that is, there is a hidden method in this person class.

  1. So when we open this person's class file, we'll see that the code for this class file looks like this.
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

public class person {
    public person() {      //We didn't write this method in the original file, but we have it in the class
                           //The method does not return a value.
    }
}

That is, a class will have a method (constructor//constructor) even if it writes nothing, which is the construction method.

You can see two features of the constructor: 1. Must have the same name as the class 2. Must have no return type and cannot write void.

2. Write a constructor

Just now our constructor was generated by default, so we can show the definition constructor.

ublic class person {
public person(){
    //Same as class name, with no return value, no write void. No parametric construct, no parametric constructor.
  }
}

3. What can a parameterless constructor do?

  1. Constructors can instantiate some initial values. For example, the following figure
public class person {
String name;
public person(){

   }
}
public class application {
    public static void main(String[] args) {
//An object is instantiated using the new keyword.
        person person = new person();
        System.out.println(person.name);

    }
}
===============================================
    The output is as follows:
null

His first function is to instantiate some initial values.

If I change the first file inside.

public class person {
String name;
public person(){
this.name="gao"
   }
}
===========================
    So when running the second file,His result is:
   gao
  1. Using the new keyword is essentially a call to the constructor.

4. parametric and parametric constructions

  • The parametric and parametric constructs are as follows
public class person {
String name;
//Parametric construction
public person(){
this.name="gao";
}
//Parametric construction
    public person(String name){
    this.name=name;
    }
}

  1. Parametric construction: Once parametric construction is available, parametric construction must display definition. Let's run it.
public class person {
String name;
//There is no display definition for parameterless construction at this time.
//Parametric construction
    public person(String name){
    this.name=name;
    }
}

public class application {
    public static void main(String[] args) {
//An object is instantiated using the new keyword.
        person person = new person();//After running, he will make a mistake here.
        System.out.println(person.name);

    }
}

What he missed was that the constructor person in the class (the address of the file) could not be applied to the specified type.

In other words, if you want to continue running this program, you must display the defined parameterless construct in the class person, as follows.

public class person {
String name;
//Parametric construction
public person(){
this.name="gao";
}
//Parametric construction
    public person(String name){  //Name in String name is a formal parameter. An argument is required to pass it something.
    this.name=name;
    }
}
  1. If we want the main program to follow the parametric construction in person, we can use the new person(); Add "Content" in parentheses and the code is as follows
public class application {
    public static void main(String[] args) {
//An object is instantiated using the new keyword.
        person content = new person("content"); //In this way he will follow a constructive approach.
        System.out.println(person.name);
    }
}

Topics: Java Back-end