Java study note 12 (Object-Oriented 5: constructor, this re-exploration), java study note
During development, you often need to specify the property values of object pairs when creating objects,
For example, a Person object should have attributes such as age and name when being created.
So how can we initialize the property value of the object while creating the object?
Here we will introduce the constructor method:
1. the constructor has no return value type, but no return value. Because it is built to an object, the execution of the method ends after the object is created.
2. the constructor name must be consistent with the type.
3. When will the constructor run? A: It is automatically executed when an object is created and only runs once.
Definition example:
Public class Person {public Person () {System. out. println ("I am an empty parameter constructor ");}}
Run:
Public class Test {public static void main (String [] args) {Person p = new Person () ;}// output: I am a constructor of an empty parameter.
The constructor is a required content of each class. Both write and write operations exist.
During compilation, the compiler (javac) checks whether there are constructor methods. If there are constructor methods, execute them, and if not, it automatically creates a constructor with null parameters.
Assignment of constructor:
public class Person { private String name; private int age; public Person(String name,int age){ this.name = name; this.age = age; }}
Public class Test {public static void main (String [] args) {Person p = new Person ("James", 18 );}}
Constructor memory analysis:
1. Run the main method in the stack. There is only one line of content: Create an object.
2. storage objects in the heap with two parameters
3. Two private variables follow into the heap memory and assign the default value (null, 0)
4. The object Passes its own address to the this keyword.
5. The object calls its own constructor and runs the constructor in the stack. The object passes in two parameters.
6. Use this method to access heap memory objects and assign values to member variables.
7. The constructor stops running, the object is created, and the address is sent to p.
The constructor can be overloaded.
Example:
public class Person { private String name; private int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; }}
Public class Test {public static void main (String [] args) {Person p = new Person ("James", 18); Person p1 = new Person ();}}
This is called between constructor methods:
Public class Person {private String name; private int age; public Person () {// this () is to call other constructor // this () this ("Zhang San", 20);} public Person (String name, int age) {this. name = name; this. age = age ;}}
Public class Test {public static void main (String [] args) {Person p1 = new Person ("James", 18); Person p2 = new Person ();}}
Memory principle:
1. Run the main method in the stack, create an object in the heap space, and assign the default value to the member variables.
2. The object calls its own constructor, calls the empty parameter constructor, runs in the stack, and passes real parameters.
3. A parameter constructor is called and a parameter constructor is called for Stack operation.
4. After the value assignment is completed, the constructor with parameters ends, the empty constructor ends, and the object is created successfully.
5. Pass the object address to p1 (p2)