static role and usage

Directory
a, static variables
two, static method
three, static code block
four, static class

It represents a static "global" or "static" means, for modifying the member variable and methods, may be formed a static static code block, but not the Java language concept of global variables. Modified static member variables and member methods independent of any object of that class, that is, it does not rely on a specific instance of the class when it declared an object class, not a copy of a static variable, all instances of the class share . As long as the class is loaded, Java virtual machine will be able to find them in accordance with the method given area class name at runtime data area. Therefore, static objects before it can access any object creation, without reference to any object.

Sometimes you want to define a class member, make use of completely independent of any object of that class. Typically, class members must be accessed through its object class, but you can create such a member, it can be used by itself, without reference to a specific instance. Before the declaration of the members with the keyword static (static) will be able to create such a member. If a member is declared as static, it can be accessed before any object of its class is created, without reference to any object. You can methods and variables are declared as static. The most common example of a static member is main (). Because the main must be called when the program started (), so it is declared as static.

static variable
in accordance with whether the static member variables of a class classification can be divided into two: one is modified static variable, called a static variable or class variable; the other is not modified static variable, called an instance variable. The difference is:

For static variable in memory only one copy (save memory), the JVM only static allocation of a memory to complete the static variable memory allocation during loading class, can be used the class name to directly access (convenience), also be implemented by objects access (but this is not recommended).

For instance variables, each create an instance, the first memory is allocated for the instance variables, instance variables can have multiple copies in memory, independently of each other (flexible).

Therefore, the general use of static variables when necessary to achieve the following two functions:

When a value between shared objects

When easy access variables

it can not be static members in the instance of the class created access, access to static member variables can be accessed via the class name, access syntax is: name of the class static variable name

static method
static methods can be invoked by class name, any instance can also be called directly, so a static method can not be used this and super keywords, you can not directly access their class instance variables and instance methods (that is, without the static member variables and members of the members of the method), can only access static member variables and member methods of their class, because instance members associated with a particular object. The method is independent of any static instance, so static methods must be implemented, and can not be abstract abstract. General class static method is to facilitate the internal calls of the other classes of methods, they can only call other static methods. Static method is a special method inside the class, method only if you need the corresponding declared as static method inside of a class are generally non-static.

Note: Reference to any instance variables are illegal in a static method. Static member method call syntax is: class static method name (argument list ...).

static final member variables and member methods for modifying, can be simply understood as "global constants." For variable indicates that once a value is not modified, and can be accessed via the class name. For the method, not covered expressed, and direct access by the class name.

static block
static block is also called a static block, a block of independent statements static class members in the class, there may be a plurality, the location can be easily put, it is not any in vivo method, the JVM executes these static load the class code block, if a plurality of static block, the JVM to execute them sequentially in the order they appear in the class, each code block is executed only once.

class static
the Java class in general is not modified with a static class, if we use static modification that kind of thing, usually modified static inner classes are nested. However, due to various reasons, such as limiting the use of factors and so on, in the practical work with not a lot. But not to say that it has no value. In some special cases static inner classes will be of great value, such as the time code of the program during the test, if a primary method are set in every Java source file, it will be a lot of extra code is present, and the most important this is the main program code file for Java, it is just a form itself does not need to master this method, but the less the main method and will never do. In this case, a method can be written to the main static inner classes, are set so that not a similar method for each master Java source file. This is useful for testing the code. In some large-scale application development, it is a commonly used techniques.

Limit static inner classes as well as general internal class differences:

Static inner classes can not access non-static members of the outer class, it can only access static members outside of class, non-static inner classes can access static and non-static member outside the class. In general, the definition of the non-static inner classes have static members to simultaneously keyword final modification, and in view of the static method can not be modified by the final, it does not define static member variables and static member method in a non-static inner classes. But you can define static member variables and member methods in a static inner classes.

Static inner classes do not have a reference to the outer class, rather than static inner class object implicitly holding a reference outside the class, point to create its external object. General non-static inner classes, you can freely access the member variables and member methods outside the class, even if the member variable or method is modified to private. But static inner classes can not access non-static member variables and member methods from outside the object class static inner classes. For example, the definition of the outer class two variables, a non-static variable, a static variable, then the static inner classes, both within member method or elsewhere, are only able to refer to the outer class Static variables can not access non-static variables.

Static inner classes do not need to create an instance of a static inner classes bound to the instance of the outer class. Typically, a founding member of the inner class in a class when there is a mandatory provision, that is, instances of non-static inner classes must be bound in the instance of the outer class. In other words, before creating the first non-static inner classes outside the class you want to use the new keyword to create the object inside the class. But when you create a static inner classes do not need bound to the instance of the outer class, that is, to define a static inner class in an external class, you do not need to use the new keyword to create an instance of the inner class.

Create a non-static inner class syntax:

OuterClass outer = new OuterClass(); 
OuterClass.InnerClass inner  = outer.new InnerClass();

Create a static inner class syntax:

OuterClass.NestedStaticClass staticInner = new OuterClass.NestedStaticClass();
Published 21 original articles · won praise 10 · views 8095

Guess you like

Origin blog.csdn.net/weixin_44997483/article/details/102851186