In Java, the constructor is called by the New keyword to be created in memory.
Use the constructor to initialize the object, and in this way you can allocate memory for the object and do other initialization work and other operations
Name of Class modifier class (parameter list)
{
Method body
}
Public, protected, default, private
Cannot have return value
Package constructor Demo;public class Apple {private String s;private Apple () {System.out.println ("constructor Run");} public static Apple Gettest () {return new Apple ();} Public String GetS () {return s;} public void Sets (String s) {THIS.S = s;}} The package constructor Demo;public class Testapple{public static void Main (String args[]) {Apple a = Apple.gettest (); A.sets (" Assigning a value to a private member variable "); String s = a.gets (); System.out.println (s);}}
My late father, class, sub-class constructor
A constructor with a parameter that has a default parameterless constructor does not perform the
public class Apple extends fruit
{
Public Apple ()
{
Call the constructor method of the parent class
Super ();
}
}
List mode two objects point to the same object.
Loads the parent class to create the object, as well as member variables and other inheritance relationships.
Loads the static and static member variables of the class, initializes it, and so on.
When static and static member variables are loaded, the object is created and the non-static member variable is loaded, initialized, and so on
Executes the body of the method in the constructor, after which the object of the class is created.
The order in which the parent class runs is the same as the order of the class
Parent class Static Statement
Sub-class static statements
Parent class constructor
Sub-class constructors
Common methods
Java (11) builder