Directory
[TOC]
1. Modifiers (access control)
default: is not write access control character
2. Static keyword 2.1 usage
Modify properties, methods, and so on
2.2 Static method 2.2.1 Static method features
- A member declared as static is saved in memory only, stored in the method area
- A member declared static will load into memory when the class is loaded, while other non-static members will allocate memory when the object is created
- Members that are modified by static can be accessed directly from the class name because they are not dependent on the object. class name. Method , class name. Property
- For some infrequently changed objects can be declared as static members, so that you can share this resource, and do not waste memory
- Some tool methods are suitable to be declared static because they can be invoked directly, but not all methods are suitable for declaration as static, because the lifetime of static members is long
- For static variables, they can be accessed through the class name and reference, which is recommended through the class name, and is not recommended for access by reference.
- Static methods, which do not have an implicit this, cannot be used in a static method to position variables in an instance. An instance method passes in an implicit this to access the instance's properties and variables
- static method Scenario: The operation is defined as a static method, such as Arrays.sort (), when the action is not related to the object (not manipulating the object property). This means that the static method is designed to provide some " tool methods " and " factory methods "
2.2.2 Case
Package Src.basic.language;import Java.util.HashSet;import Java.util.Iterator;/** This class mainly speaks of the use of the static keyword. * */ Public classStatickeywords {Static intParamstatic =0;intParamclass; Public Static void Main(String [] args) {Hashset<string> set =NewHashset<string> (); Set.Add("Rolling"); Set.Add("Refueling"); Set.Add("roll Forward");//For static methods, properties, you can not instantiate the class, you can also callStatickeywords.traversetwithlist(set); Statickeywords.Traversewithiterator(set);//The member declared as static saves only one copy in memory, and if the value of the static variable is modified in one place, the value of the other object references the property will be modified as well . /** This example is omitted, you can declare a static variable in the class of the call, and then instantiate two variables of this type in this class to observe * */} Public Static void traversetwithlist(HashSet <String> Set) {Object [] Objs = set.ToArray(); for(Object Obj:objs) {System. out.println(obj); Set.Add("roll One More time."); } System. out.println(); } Public Static void Traversewithiterator(HashSet <String> Set) {//In the static method, there is no food incoming this, the following statement error //This.paramclass = 1; //Static properties are determined by the class name. property.Statickeywords.paramstatic= -; Iterator<string> ITR = set.iterator(); while(ITR.Hasnext()) {System. out.println(ITR.Next()); } System. out.println(); } Public void teststatic() { This.Paramclass= -; Statickeywords.paramstatic= -; } }
2.3 Static Block
A block of code that is part of a class that executes only once and can be used to load static resources in the software during class loading
Application Scenarios:
- Often used to load static resources (pictures, audio, etc.)
2.3.1 Case
publicclass StaticKeywords { // 定义了一个静态块 static { // 存储在方法区中,在类加载的时候执行的代码块 System.out.println("静态块执行"); } publicstaticvoidmain(String [] args) { }}// 执行结果 静态块执行 // main函数中没有任何代码,但是还是打印了静态块的语句,说明静态块在所有类实例化之前执行
3. Final keyword
Final-Modified member variables and methods cannot be changed
- Modifier variable: variable cannot be changed
- There are two ways to final modify member variables:
- Simultaneous initialization of declarations
- Initialization in constructor method
- Final Modification Method variables
- When the final modification method variable, the definition can not be assigned value, only need to be assigned before use, can not be modified after assignment
- Modification method: Method cannot be overridden
- Decorated classes: Classes cannot be inherited, but other classes can be inherited
"' Java
Package src.basic.language;
public class Keywordsfilnal {
Declaring a modified variable at the same time
Final int a = 5;
// 声明的同时若没有初始化,要在构造函数中初始化final int b;public KeyWordsFilnal() { // TODO Auto-generated constructor stub // 构造方法中初始化 b = 10;}void finalParam() { // 在类方法中, final int c; // 报错,使用之前要赋值 // System.out.println(c); c = 6; // 报错 // c = 7;}final void finalMethod() {}public static void main(String [] args) { KeyWordsFilnal obj = new KeyWordsFilnal();}
}
The final class can inherit from other classes
Final class Keywordsfinalchild extends keywordsfilnal{
Error, final modified method cannot be overridden
void Finalmethod () {}
}
Error, final modified class cannot inherit
Class Keywordsfinalgrandchild extends keywordsfinalchild{}
```
4. Static Final constant
A member variable that is modified by static final is a constant and must be initialized at the same time as the declaration and cannot be changed. Constants that are modified by the static final are replaced during compilation.
4.1 Rules and recommendations
- Variables that are modified by static public must be assigned at the time of declaration
- The constant that accesses the static public modifier is the same as the static variable for the static keyword decoration
- static public-modified constants cannot be changed
- Recommended constants all letters are capitalized
- Automatically replaced with specific values at compile time, equivalent to macros in C and C + +, performing more efficiently
4.2 Cases
package src.basic.language;class KeywordsStaticFinal { publicstaticvoidmain(String [] args) { // 在编译时,程序还未执行前,KeywordsStaticFinal.PI被替换为3.14159 // 这句话和System.out.println(3.14159)是一样的,由于少了一步转换的操作,使用常量程序运行效率更高 System.out.println(KeywordsStaticFinal.PI); } publicstaticfinaldouble3.14159; // 编译错误,static public修饰的变量必须在声明时就赋值 // public staic final int NUM;}
Java Basic 01 Access control, Static, final, and static final