java Constructor Default Constructor and Parameterized Constructor
- 2021-11-24 01:27:05
- OfStack
1.2 Display Definition Constructor
1. Constructor
Constructor, also called constructor, constructor, is a special type of method, which is responsible for the initialization of member variables (fields) in a class. The constructor is used to perform initialization when an object is created. When an object is created, the system initializes an instance of that object by default.
There are two types of constructors:
1. Default constructor (no parameter constructor) 2. Parametric construction method
1.1 The permission modifier is public
The permission modifier is
public
That indicates that internal properties can be accessed by other classes.
java
The default constructor is parameterless.
The Java compiler automatically creates a parameterless constructor, so we can omit parameterless constructions in classes even if they don't exist. You do not need to assign a value when instantiating an object
Create a new one
Person
Class, and then instantiate it with new
person
Object. After running, it will generate 1
Person.class
Object.
public class Demo02 {
public static void main(String[] args) {
//new Instantiation 1 Objects
//1 A class exists even if it doesn't write anything 1 Methods with the same method name as the class name.
Person p=new Person(); Create an object without assignment in parentheses , Without assignment, , Character type defaults to null, The default value of numeric type is 0
}
}
public class Person {
}
If nothing is written in the class, it will be 1 more by default after running
Person()
Method, the following is
Person.class
Code of:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package Demo01;
public class Person {
public Person() {
}
}
Therefore, even if a class writes nothing, it will still have a default method
Parameterized construction method:
1.2 Display Definition Constructor
package Demo01;
public class Demo02 {
public static void main(String[] args) {
//new Instantiation 1 Objects
//1 A class exists even if it doesn't write anything 1 Methods with the same method name as the class name.
Person p=new Person();
System.out.println(p.name);//null
}
}
package Demo01;
public class Person {
String name;
// Instantiate initial value
//1 , using new Keyword, essential reality constructor
//2 Which is used to initialize the value
public Person(){// Default constructor
this.name="xiaoming";
}
// Parametric structure: 1 Once a parametric construction is defined, a parametric construction must show the definition, otherwise it is invalid , Also overloaded
public Person(String name){
this.name=name;
}
// Shortcut keys: alt + insert Generate constructor, automatically generate constructor, and choose whether there are parameters or not.
}
Summary:
Constructor:
1. Same as class name 2. No return value
Function:
1. new essentially calls the constructor 2. Initialize the value of the object
Note: After defining a construct, if you want to use a parameterless construct, the one shown is a parameterless construct; Otherwise, you cannot use the parameterless constructor ALT + INSERT to generate the constructor