当前位置:网站首页>Process analysis of object creation

Process analysis of object creation

2022-07-27 03:49:00 Jiangbei - Science and technology

Object creation process analysis

 Introducing constructors

Let's take a look at a case

class Person  //  class  Person
{
    
    int age = 90;
    String name;

    Person(String n,int a)  //  Constructors 
    {
    
        name = n;  //  Assign a value to a property 
        age = a;   // ...
    }
}

Person p=new Person(" Small qian ", 20);

stay JVM Memory analysis
 Insert picture description here
Process analysis ( The order must not be disordered )

  1. First, it will be loaded in the method area Person Class information ( Person.class), It will only load this time
  2. Conduct new Person(“ Small qian ”,20) Allocate space in the heap ( Address ), Create objects
  3. Complete object initialization
    a. Default initialization age = 0 name = null
    b. Explicitly initialize age = 90 name = null
    c. Constructor initialization age = 20 name = Small qian
  4. Put the address of the object in the heap , Return to P(P Is the object name , It can also be understood as a reference to an object )

 Constructor not introduced

Deepen understanding and backtracking
 Insert picture description here

Code
 Insert picture description here

OK, This issue of sharing is here , Comments are welcome , Thank you for your support

原网站

版权声明
本文为[Jiangbei - Science and technology]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/208/202207270002062641.html