Java's static understanding

Source: Internet
Author: User

Speaking of the keyword static, the first thought of the constant, static variables, this article I summed up the use of static.

1. Static variables

Can be assigned for easy class access.

2. Static method

Both static and static variables can be modified by private and public.

When the JVM loads a class, it loads the static and static variables of the class into the method area. and is only loaded once, shared with all threads, and other threads can change static variables in the method area.

Static final can modify variables and methods to indicate that constants cannot be modified, that methods are not overwritten, and can be accessed through the class name.

3. Static code block

When the JVM loads a class, the static code block runs only once. Because it is loaded into the heap memory first, and the constructor of the class is loaded at new, the constructor runs after the static code block

Non-static variables and non-static methods are not directly accessible inside static code blocks. You can call a method or assign a value to a variable of its class by instantiating the class of the method.

4, there is an internal class called Static inner class

Static inner classes do not need to rely on external classes, and non-static inner classes must hold references to external classes.

5, static Guide package, import static. Simplify the code.

    

Java's static understanding

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.