static means "global" or "static", is used to modify members variables With member methods, static code blocks can also be formed, but There is no concept of global variables in the Java language.
Member variables and member methods modified by static are independent of any objects of the class. That is, it does not depend on a specific instance of the class and is shared by all instances of the class. As long as this class is loaded,
The Java virtual machine can determine the method area in the runtime data area based on the class name. Find them. Therefore, a static object can be accessed before any of its objects are created, without referencing any objects.
The static member variables and member methods modified with public are essentially global variables and global methods. When an object of its class is declared, a copy of the static variable is not generated, but All instances of a class share the same static variable.
static variable can be preceded by private modification, indicating that this variable can be used in the static code block of the class, or in other static member methods of the class (of course it can also be used in non-static member methods used in --- nonsense), but it is important not to directly reference it through the class name in other classes. In fact, you need to understand that private means access permission restriction, and static means it can be used without instantiation, which is much easier to understand. The effect of adding other access rights keywords in front of static is also the same.
Some characteristics of static:
(1) Static variables are also called static The difference between variables, static variables and non-static variables is that static variables are shared by all objects and have only one copy in memory. It will be initialized when and only when the class is first loaded. Non-static variables are owned by the object and are initialized when creating the object. There are multiple copies, and the copies owned by each object do not affect each other.
(2) statice has two functions: first, it only wants to allocate a single storage space for a specific domain, regardless of how many objects are created, or even no objects are created; second, , hope that a method is not associated with any object containing it, that is, any method in it can be called without creating any object.
(3) Any member variables and member methods modified by statice are independent of any object of the class. It does not depend on any object of the class and is shared by all instances.
(4) Static variables have only one copy in the memory. JVM only allocates static memory once. The memory allocation for static variables is completed during the process of loading the class. You can use the class name to directly access.
(5) Static code is bound to class. Successful loading of class means your static code has been executed and you will not do this again in the future. Static code. The function of Class.forName() is to ask the JVM to find and load the specified class, which means that the JVM will execute the static code segment of the class.
(6) Static method Attribute initialization is initialized when the class is loaded. Instead of static method property initialization.
Example:
public class Test { static int i; static { i++; System.out.println("outt i:"+i); } static void outi() { i++; System.out.println("output i:"+i); } public static void main(String [] argStrings) { Test test=new Test(); test.outi(); } }
##Output result:
outt i:1 output i:2
##
The above is the detailed content of Detailed introduction to Java keyword static. For more information, please follow other related articles on the PHP Chinese website!

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

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.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor