The role of the static keyword in Java

Source: Internet
Author: User

There are four usage scenarios in the Java language: member variables, member methods, code blocks, and inner classes

(1) static member variable

In Java, you can achieve the effect of global variables by statin keyword modifiers. Static modified variables are classes that are allocated memory space the first time a class is passed through the class loader to the JVM.

(2) static Member method

The static modified method belongs to a class method and can be called without creating an object. Keywords such as this and super cannot be used in the static method and cannot be called a non-static method, only static member variables and static methods of the owning class can be accessed.

(3) Static code block

The JVM executes a static block of code when it loads the class, which is often used to initialize the statically variable, and the static code executes and executes only once when the class is loaded.

(4) Static inner class

The static inner class can be instantiated without relying on an external class instance object, while an inner class needs to be instantiated after an external class instantiation.

Static inner classes cannot access ordinary variables of external classes, only static member variables and static methods of external classes.

The role of the static keyword in Java

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.