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.
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!

The article discusses various Java garbage collection algorithms (Serial, Parallel, CMS, G1, ZGC), their performance impacts, and suitability for applications with large heaps.

The article discusses the Java Virtual Machine (JVM), detailing its role in running Java programs across different platforms. It explains the JVM's internal processes, key components, memory management, garbage collection, and performance optimizatio

Java's Nashorn engine enables JavaScript scripting within Java apps. Key steps include setting up Nashorn, managing scripts, and optimizing performance. Main issues involve security, memory management, and future compatibility due to Nashorn's deprec

Java's try-with-resources simplifies resource management by automatically closing resources like file streams or database connections, improving code readability and maintainability.

Java enums represent fixed sets of values, offering type safety, readability, and additional functionality through custom methods and constructors. They enhance code organization and can be used in switch statements for efficient value handling.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use