Static usage in Java and Javastatic usage
Static, as modifier, is initially introduced by c. static at the beginning indicates that the local variable still exists after exiting a block. Subsequently, static indicates global variables and functions that cannot be accessed by other files. In C ++ and java, static indicates the variables and functions that belong to the class and do not belong to the class object.
From the specific usage, it mainly uses five aspects.
1. Static domain.
The static modifier class has only one field (field, member variable) in the class. It belongs to the class, also known as the class field. It can only be operated by classes and cannot be operated by objects. For example
Pubic class Employee
{
Private static int nextId = 1;
Private int id;
...
}
NextId is a static domain, and id is an instance domain. NextId only has one copy in the class "Employee". All the Employee objects share this static field. Even if there is no object, nextId still exists. The instance domain id can have an id for different objects. The id belongs to the object and does not belong to the class. During the operation, use the class to operate static fields, that is, Employee. nexdId. Use an object to operate the instance domain, that is, tom. id (tom is an object of Employee ).
For static fields, static variables are usually used less, and static constants are used more. A typical example is PI in mathematics.
Public class Math
{
Public static final double PI = 3.14159265358979323846;
}
The program obtains this constant through Math. PI.
Ii. Static Methods
Static methods are static methods, and static methods belong to Classes. A typical example is the method in Math, such as pow (x, a), which indicates the power of x. When calling it, write Math. pow (x, a) directly ).
Static methods cannot operate on objects, nor access instance domains. They can only access static domains of their own classes. For example
Public static int getNextId ()
{
Return nextId; // return the static domain
}
3. Static code block
Inside a class, a code block modified with static is also called a static initialization block. It is mainly used to initialize static fields inside the class. It is executed only once when class is used for the first time and before the constructor. For example, before connecting to the database, you need to initialize the strings used to connect to the database, and load the driver only once, put these in the static code block. The sample code is as follows: (part of the code that can be used to illustrate the problem)
Static
{
// Name of the driver to be loaded
Drivername = prop. getProperty ("drivername ");
// Database address
Dburl = prop. getProperty ("dburl ");
// Database username
Dbusername = prop. getProperty ("dbusername ");
// Database Password
Dbpassword = prop. getProperty ("dbpassword ");
// Load the driver only once
Class. forName (drivername );
}
Iv. Static Import
When the import class is added with static, You can import static fields and static methods.
Add the following code to the top of the source file:
Import static java. lang. Math .*;
In the subsequent programs, we can directly use the static domain and static method of Math.
For example, double I = pow (PI, 3 );
In this way, the code is better than the code before static import.
Double B = Math. pow (Math. PI, 3 );
It is much clearer and concise, making it easier to understand.
5. static internal class
The static class is added before the internal class. This method only hides this class inside another class and does not need to reference peripheral class objects. Objects of static internal classes are identical to other internal classes except those of peripheral classes.
When I read the core Java technology Volume I, I learned other things and summarized the static usage. please correct me.