Static domain
?? If you define a domain as static, there is only one such domain in each class. Each object has a copy of its own for all instance domains. For example, suppose that you need to give each employee a unique identifier, add an instance domain ID and a static domain NextID to the employee class:
class Employee{ privatestaticint1; privateint id;}
?? Now, each employee object has an ID field of its own, but all instances of this class share a nextid. NextID belongs to a class, not to any object, there is no object, and the static domain NextID exists.
?? For static variables, static constants are more common in Java, and in our usual familiar Math class we define static constants for direct use:
publicfinalclass{ publicstaticfinaldouble2.7182818284590452354; publicstaticfinaldouble3.14159265358979323846;}
For a static domain, its usage scenarios are typically used for:
- To share data between objects
- Easy access
Static methods
?? Again, we can still see a lot of static methods in the Math class:
publicfinalclass{ publicstaticdoublesin(double a) { return StrictMath.sin(a); } publicstaticdoublecos(double a) { return StrictMath.cos(a); }}
?? For the invocation of a static method, we can use the "class name. Method Name" way, or you can use the "object name. Method Name" In a way called, static methods when accessing members of this class, only allow access to static members (that is, static variable (constant) and static methods), and not allow access to the instance members, Because instance members are associated with specific objects, static methods belong to the class and are not bound to any objects.
Static code block
?? Multiple static initialization actions can be organized into a special "static clause" to form a static block of code:
publicclass Test{ staticint i; static{ 1; }}
For the code above, it is actually a code that follows the static keyword, which, like other static initialization actions, executes only once: When an object of this class is first generated, or when a static data member belonging to that class is first accessed (even if the object of that class has never been generated)
classcup{Cup(intMarker) {System. out.println("Cup ("+ marker +")"); }void F(intMarker) {System. out.println("F ("+ marker +")"); }}classcups{StaticCup Cup1;StaticCup cup2;Static{Cup1 =New Cup(1); Cup2 =New Cup(2); }Cups() {System. out.println("Cups ()"); }} Public classexplicitstatic { Public Static void Main(String... args) {System. out.println("Inside Main ()");//cups.cup1.f (99); (1)Cups cups =New Cups();//(2)}}/* Execute the Code at (1) with the following output:Inside Main ()Cup (1)Cup (2)F (a)Execute the Code at (2) with the following output:Inside Main ()Cup (1)Cup (2)Cups ()*/
It is important to note that for code that executes (2), the content in the static code block is better than the constructor performed first . In addition, if (1), (2) of the code is not executed, then the code in the static code block will not be executed.
Static and final
Static and final are decorated with members, which are generally used as global constants, whose modified variables cannot be modified, and the decorated methods cannot be overwritten.
Static common face question
- Why there is no static property and static method inside a non-static inner class
The properties and methods of the static type are present in memory when the class is loaded, and the static property or method of a class is loaded into the JVM, and the non-static inner class defaults to holding the reference to the external class and relies on the external class, so if a non-static An inner class that once has a static property or method will appear when an inner class is not loaded but tries to create the static property and method of the inner class in memory, which is naturally wrong, and the class does not exist (not loaded) but wants to manipulate its properties and methods. From another point of view, the non-static inner class is loaded (not automatically following the main class) when instantiated, and the semantics of static is that the class can access the static property or method of the class directly through the class name, so if an inner class that does not instantiate a non-static is equal to the non-static The inner class is not loaded, so it is impossible to talk about accessing the static property or method through the class name.
Use of static in Java