Initialization process for Java learning objects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.