“This is the third day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”
Static variables
Static int data statement data is a class variable. It is a shared variable of a class and belongs to the entire class.
Ex. :
Class M{
static int data;
}
M m1=new M(); M m2=new M();
m1.data=0;
Copy the code
M1. data++ is 1, and m2.data is also 1.
Static defines a storage area that is common to the entire class.
Its variables can be accessed by the class name: class name. The variable name. Is equivalent to accessing a variable through an object reference.
Second, static method
Public static void printData(){}
Indicates that such methods are class methods (static methods)
Static methods do not need to have objects and can be called using the class name.
Static methods do not allow access to non-static members of a class, including their variables and methods, because they are called through the class and have no concept of an object. The this.data and super.data methods are not available.
The reason: Basically, static variables exist whether the class is instantiated or not, whereas instance variables exist only if the class is instantiated. The existence of an instance variable is not determined when a static method is called directly.
Normally, the main method is a static method, so the JVM can call it directly. The main method is static because it is an entry point to the entire software system, and there are no objects in the system to enter, so only class calls can be used.
Guess: The JVM has a statement like this in its code:
ClassName.main(arg);
Copy the code
ClassName is obtained from the Command line “Java class name”, so the class name does not need the. Class extension
* Overrides do not apply to static methods.
Static methods cannot be overridden.
If a subclass has a static method with the same name as its parent class, it will compile, but it is not polymorphic, so it is not an override.
public class Test { public static void main(String[] arg) { Super s = new Sub(); s.show(); } } class Super { static public void show(){System.out.println("in Super"); } } class Sub extends Super { static public void show(){System.out.println("in Sub"); }}Copy the code
The result is in Super
Static inner class —- can only be a member inner class
class Out{
public static class Inner{}
}
Copy the code
Initialization block
1. Is executed only once;
2. The initialization block is run first after the class is loaded, whether the class is instantiated or not
3. Typically used to initialize static variables
Public static void main(String[] args){ System.out.println(Car.name); // Load the Car Class into the JVM and perform static generation // code block}Copy the code
5. Singleton pattern
Static is usually used for Singleton pattern development:
Singleton is a design pattern, above grammar, that ensures that a class has only one object in the entire system.
Features:
1. There’s a static property
2. Private constructs — Singleton cannot be new
3. Public static methods to obtain static properties
How to implement the singleton pattern:
Using the unique properties of class attributes (static variables) in the system, it creates such a unique reference and controls the space to which the reference refers.
public class ConnectionFactory{ private static Connection conn; private Connection(){ if(conn==null) conn = new Connction(); } public Connection getInstance(){ return conn; }}Copy the code
The 2
public class ConnectionFactory{ private static Connection conn; static{ conn = new Connection(); } public static Connection getInstance(){ return conn; }}Copy the code
\