Home >Java >javaTutorial >Can java static variables be assigned values?
#Can java static variables be assigned values?
can be assigned a value. The main function of static is a static member, which means that the instance of the variable is stored once in the memory. The assignment can be changed at will.
static keyword in java
static is a very important keyword in java and has two main functions:
●First: Allocate a single storage space for a specific data type or object, regardless of the number of objects created.
●Second: Implement a method or attribute associated with a class rather than an object
Simply speaking, in the Java language, static has 5 main uses: member variables, Member methods, code blocks, inner classes and static import packages.
Basic usage:
1. Static modified member variables: This member variable belongs to a class variable and can be directly referenced through ClassName.attributeName without the need for new out of the class. Example.
2. Static modified member method: This method belongs to the class method and can be directly referenced through ClassName.methodName without the need for a new instance out of the class.
3. Static modified code block: It is only executed once when the class is initialized, and the loading order is strictly in accordance with the definition order of the static resources in the class; the static code block is for the static variables defined after it , can be assigned, but cannot be accessed. ;Parent class code block->Subclass code block.
4. Static modification of inner classes: static cannot modify ordinary classes, but can only modify inner classes. The creation method of inner classes modified by static is: new OuterClass.InnerClass().
5. Static import package: syntax "import static java.lang.Math.*", so that you can directly use the static methods in the Math class in the class without writing the class name. In my opinion, It is more convenient when a certain class is used frequently, but it reduces readability. And it is not recommended to import *
Recommended learning: Java video tutorial
The above is the detailed content of Can java static variables be assigned values?. For more information, please follow other related articles on the PHP Chinese website!