1. final and static summary
final and static often appear in our programs. When and under what scenarios?
static final is usually used for constants that need to be used in many parts of the project
public static final int DEF_CONN_TIMEOUT = 30000; public static final int DEF_READ_TIMEOUT = 30000;
Many classes need to use tool classes, which also need to be set to static
public static JSONObject getDayDetail(int year,int month,int day){ JSONObject JSONResult = null; }
2. final (modifier)
2.1 final modification class
Modify the class with final to indicate that this class cannot be inherited
2.2 final modification method
The reason for using the final method is to lock the method to prevent any inherited class from modifying its meaning;
That is, the final method of the parent class cannot be inherited and overridden by the subclass.
2.3 final modifier variable
The final member variable represents a constant. Once assigned, the value cannot be changed
When final modifies a basic data type, the value of the basic data type cannot be changed after initialization.
When final modifies a reference type, it cannot point to other objects after initialization, but the content of the object can be changed.
3. static (keyword)
**static keyword function: * * call methods / variables without creating objects
static modified methods or variables do not need to rely on objects for access, as long as the class is loaded.
3.1 static modification method,
I.e. class method
public class A{ public static int a = 1; public static void print(){ System.out.println("hello"); } } public class B{ public static void mian(String[] args){ A.print(); System.out.println(A.a); } }
3.2 static variable
That is, static variables, which can be shared by all objects.
There is only one copy of static variables in memory (save memory). The JVM allocates memory only once for static variables. During the process of loading classes, the memory allocation of static variables can be directly accessed by class name (convenient). Of course, it can also be accessed through objects (but this is not recommended).
For instance variables, memory will be allocated for instance variables every time an instance is created. Instance variables can have multiple copies in memory without affecting each other (flexible).
static member variables are initialized in the defined order. static cannot modify local variables;
Therefore, static variables are generally used when the following two functions need to be realized:
• when sharing values between objects
• easy access to variables
3.3 static code block
Static keyword also plays a key role in forming static code blocks to optimize program performance. Static blocks can be placed anywhere in the class, and there can be multiple static blocks in the class. When the class is first loaded, each static block is executed in the order of static blocks, and only once.
The reason why static block can be used to optimize program performance is because of its characteristic: it will only be executed once when the class is loaded. Here is an example:
class Person{ private Date birthDate; public Person(Date birthDate) { this.birthDate = birthDate; } boolean isBornBoomer() { Date startDate = Date.valueOf("1946"); Date endDate = Date.valueOf("1964"); return birthDate.compareTo(startDate)>=0 && birthDate.compareTo(endDate) < 0; } }
isBornBoomer is used to determine whether the person was born in 1946-1964. Each time isBornBoomer is called, two objects, startDate and birthDate, will be generated, resulting in a waste of space. If this is changed, the efficiency will be better:
class Person{ private Date birthDate; private static Date startDate,endDate; static{ startDate = Date.valueOf("1946"); endDate = Date.valueOf("1964"); } public Person(Date birthDate) { this.birthDate = birthDate; } boolean isBornBoomer() { return birthDate.compareTo(startDate)>=0 && birthDate.compareTo(endDate) < 0; } }
Therefore, many times, some initialization operations that only need to be performed once will be carried out in the static code block.