Detailed in Java static


Click to view original


"Java programming ideas"
"in no way is this a static method. Can not call non-static methods inside the static method, in turn, is possible . And you can not create any objects under the premise, only to call the static method through the class itself this fact is the main purpose of static methods. "

In short:
to facilitate the type of call static variables or methods without creating an object down. It is clear that the modified variable or static keyword method does not depend on the object to be accessed. As long as the class is loaded, you can go accessed by class name. static methods can be used to modify a member of the class, the class member variables, static block may additionally write the program to optimize performance.

1) static method
is generally called a static method. Because they do not depend on any object can be accessed. So for static methods, is not this the. Because of this feature, non-static member variables and methods can not access non-static member class in a static method, but points static member methods can access static member methods / variables.

2) static variables
, also known as static variables. The difference between static and non-static variables are variables: Static variables are shared by all the objects, only one copy in memory, if and only if it will be initialized when you first load the class. Rather than static variables are objects have been initialized when the object is created, there are multiple copies of each object has a copy of each other. initialization sequence static member variables are initialized defined sequence.

. 3) static block
static keyword there is a more critical role in forming a static code block is used to optimize the performance of the program. static blocks may be placed anywhere in the class, the class may have a plurality of static blocks. When the class is loaded for the first time, each of the static block may be performed in the order of static blocks, and only once.

Why static block can be used to optimize application performance, because of its characteristics: only once when the class is loaded.

And C / C ++ static in comparison
with C / C ++ static in different, Java's static keyword does not affect the scope of a variable or method. In Java can affect access only private, public, protected.

Guess you like

Origin www.cnblogs.com/Actstone/p/java-zhong-static-xiang-jie.html