Home  >  Article  >  Java  >  What is the function of static keyword in java

What is the function of static keyword in java

coldplay.xixi
coldplay.xixiOriginal
2020-12-14 09:07:3336190browse

The role of the static keyword in java: 1. In java, variables can be modified by the statin keyword to achieve the effect of global variables; 2. The method modified by static is a class method and can be called without creating an object; 3 , static code block is often used to initialize static variables.

What is the function of static keyword in java

The operating environment of this article: windows10 system, java 1.8, thinkpad t480 computer.

The role of the static keyword in java:

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

(1) static member variables

In java, variables can be modified by the statin keyword to achieve the effect of global variables. Variables modified by static (static variables) belong to the class and are allocated memory space when the class is first passed to the jvm through the class loader.

(2) static member method

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

(3) static code block

The JVM will execute the static code block when loading a class. The static code block is often used to initialize static variables. The static code will only be executed when the class is loaded. once.

(4) static inner class

Static inner class can be instantiated without relying on the external class instance object, while the inner class needs to be instantiated after the outer class is instantiated.

Static inner classes cannot access ordinary variables of the outer class, but can only access static member variables and static methods of the outer class.

Related free learning recommendations: java basic tutorial

The above is the detailed content of What is the function of static keyword in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn