Example code:
1 class Person2 {3 PrivatePerson () {}4 PrivateString name = "Hah";5 Private intAge ;6 Private StaticString country = "CN";
constructor function7Person (String name,intAge )8 {9 This. Name =name;Ten This. Age =Age ; One }
Static code block A Static - { -System.out.println ("Hello, man"); the }
Building code Blocks - { -System.out.println (name+ "..." +Age ); - } + Public voidsetName (String name) - { + This. Name =name; A } at
Non-static functions - Public voidspeak () - { -System.out.println ( This. name+ "..." + This. age); - } -
static functions in Public Static voidshowcountry () - { toSystem.out.println ("country=" +person.country); + Person.method (); - } the Public Static voidmethod () * { $System.out.println ("Method Run");Panax Notoginseng } - the } + A classPersondemo the { + Public Static voidMain (string[] args) - { $Person p =NewPerson ("Zhangsan", 20); $P.setname ("Lisi"); - NewPerson (); - } the}
For example: person p = new person ("Zhangsan", 20);
What has been done in this sentence?
1) because new uses the Person.class. The Person.class file is first found and loaded into memory.
2) Execute the static code block (if any) in the class to initialize the Person.class class.
3) Open space in heap memory, allocate memory address (16 binary: 0x0015).
4) Establish the unique properties of the object in heap memory. and the default initialization. (e.g.: name = NULL; age = 0;)
5) display initialization of the properties. (for example: name = "haha";)
6) constructs the code block initialization for the object.
7) Initialize the corresponding constructor function for the object.
8) Pay the memory address to the P variable in the stack memory.
Initialization process for Java learning objects