Java Basics-Initialization of classes and objects

Source: Internet
Author: User

This section elaborates on one side of all the code blocks involved in the initialization of classes and objects.

Example code:

1  Public classDemo {2     Private StaticString name;3     PrivateString age;4 5     //Main function6      Public Static voidMain (string[] args) {7Demo Demo =NewDemo ();8 9     }Ten  One     //constructor Function A      PublicDemo () { -System.out.println ("I am a constructor that runs as the object is built"); -     } the  -     //I'm building blocks of code . -     { -System.out.println ("I'm building a block of code that runs as the object builds, takes precedence over the object's constructor"); +  -     } +     //I am a static block of code A     Static { atSystem.out.println ("I am a static block of code that runs as the class loads"); -     } -}

Create a new demo class, the main function of the new such object demo;

To run the steps:

While the 1.Demo class loads, the static code blocks are loaded and executed together, and the function is to initialize the class

The 2.new keyword creates space in the heap, allocates memory addresses, and establishes unique properties for the object. The default initialization is performed, and the properties are displayed initialized.

3. Initialization of the construction code block takes precedence over the constructor execution, which is initialized for all objects;

4. Following the execution of the constructor function, the function is to initialize the related object;

Java Basics-Initialization of classes and 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.