Static code block

Source: Internet
Author: User

What is the difference between a member variable and a local variable?

Member variables:

1. Member variables are defined in the class and can be accessed throughout the class.

2. Member variables are established with the creation of the object and exist in the heap memory where the object resides.

3. member variables have default initialization values.

Local variables:

1. Local variables can be defined in the local scope, such as: Inside the function, within the statement and so on.

2. Local variables exist in stack memory

3. The scope of the action ends, and the variable space is automatically released.

4. Local variables do not have default initialization values

Why is the constructor not returning a value?

The constructor cannot return anything else, only the object that was just created, so its behavior is fixed.

Code block:

1. Local code block

Define {} Inside the function,

Cannot use static decoration

2. Construct a code block (which itself means a supplement to the constructor)

is also a member of a class, and a member variable and member function sibling.

When constructing an object, the construction code block is called first and then the constructor.

3. Static code block

Construct code block + static modifier---> Static code block

A static block of code executes when the class is loaded and executes only once

The execution time of a static code block is when the class is loaded

Class.forName (String,boolean,initialize,classloader) The second parameter can control whether static code blocks are called when loading, false by default


Note: Static code blocks are executed when the class is loaded, rather than when the object is created, and a class is loaded into a place called the "method area" where the class information is stored permanently

When a virtual machine runs, it creates a run time zone that includes the allocated stack space and method area in the area, and the method area placement class information creates a new stack area each time a thread is created, the stack is responsible for storing the class's methods, and each method is pressed into the top of the stack when the method Pop it up when you're done. A run time zone requires at least one stack to hold the Main method, which also means that a program requires at least one thread to execute the main method.













Static code block

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.