JAVA's static variables, static methods, static classes

JAVA's static variables, static methods, static classes

Static variables and static methods belong to static objects, and the difference between them and non-static objects needs to be explained.

What is the difference between Java static objects and non-static objects?

The comparison is as follows:

Static object

Owned attributes: are shared by the class

Memory allocation: the memory space is fixed

Allocation sequence: allocate space for static objects first

Non-static object

Owned attributes: are independently owned by each object of the class

Memory allocation: Space is allocated in each subsidiary class

Allocation sequence: Then allocate space for non-static objects, that is, the initialization sequence is first static and then non-static.

What are the benefits of java static objects?

(1)

A. The data of a static object is unique globally, and it will be changed once it is changed. If the thing you want to deal with is unique in the entire program, it is a good way to make it static. After you modify the non-static thing, you only modify its own data, but it will not affect the data of other similar objects.

B, easy to quote. Directly use the class name, static method name or class name. static variable name to quote and modify its attribute value directly, without the get and set methods.

C, to maintain the uniqueness of the data. This data is globally unique. Any place that is modified will reflect the modification of these data in all the places used in the program. Effectively reduce unnecessary waste.

D, static final is used to modify member variables and member methods, which can be simply understood as "global constants". For variables, it means that once the value is given, it cannot be modified; for methods, it means that it cannot be overwritten.

(2) Static variables, static methods and static blocks

Under normal circumstances, a class member must be accessed through the object of its class, but it is possible to create a member that can be used by itself without having to refer to a specific instance. Add the keyword static in front of the member declaration to create such a member. If a member is declared as static, it can be accessed before any objects of its class are created without having to refer to any objects (it has nothing to do with whether the class has static modification).

You can declare both methods and variables as static. The most common example of a static member is main( ). The method declared as static has the following restrictions (also for main):

A, they can only call other static methods

B, they can only access static data

C, they cannot refer to this or super in any way (this refers to objects, super is related to inheritance)

Example: The Static block is executed only once when the class is loaded. The following example shows that the class has a static method, some static variables, and a static initialization block.

public class TestNew {  
    static int a = 3;  
    static int b;  
    static void meth(int x){  
        System.out.println("x = "+x);  
        System.out.println("a = "+a);  
        System.out.println("b = "+b);  
    }  
    static {  
        System.out.println("static block initialized");  
        b = a*4;  
    }  
    public static void main(String[] args) {  
       //TODO Auto-generated method stub  
        meth(42);  
    }  
}
 

The results of the implementation are:

static block initialized

x = 42

a = 3

b = 12
 

The execution sequence of the above class TestNew is: first the static block is executed (printing a message), a is set to 3, and finally b is initialized to a*4 to 12. Then call main(), main() calls meth(), passing the value 42 to x. The three println () statements refer to two static variables a and b, and local variable x.

(3) Use static variables or static methods externally

Outside of the class in which they are defined, static methods and variables can be used independently of any object, as long as you add the dot operator after the class name. As you can see, this format is similar to the format of calling a non-static method or variable through an object reference variable. Example:

class StaticDemo{  
    static int a = 42;  
    static int b = 99;  
    static void callme(){  
        System.out.println("a = "+a);  
    }  
}  
public class TestNew {  
    public static void main(String[] args) {  
       //TODO Auto-generated method stub  
        StaticDemo.callme();  
        System.out.println("b = "+StaticDemo.b);  
    }  
}  
 

Results of the:

a = 42
b = 99
 

(4) A static inner class means that a class decorated with static is defined inside a class. It can be understood with the structure in C in the embedded structure.

A. First of all, the internal class is used because the internal class has a certain relationship with the external class in which it is located, and often only the external class calls this internal class. So there is no need to store this class in a Java file.

B, static methods, static member variables. Its only function is to produce with the loading of the class (rather than with the production of the object), so that it can be directly obtained with the class name + static member name. In this way, the static inner class can be understood, and it can be directly obtained by using the outer class name + inner class name. Examples are as follows:

public class Build {
     public static class VERSION {
            public static final String RELEASE = getString("ro.build.version.release");
      }
}
 

It can be accessed directly from the outside through Build.VERSION.RELEASE.

One addition: about the definition of class variables in the class (not related to the static theme). WakeLock is a class in the PowerManager class, which has been imported Android.os.PowerManager; if you want to define WakeLock variables, you need PowerManager.WakeLock mSmartStayWakeLock = null; if you want WakeLock mSmartStayWakeLock = null; then you need to import android.os.PowerManager when importing .WakeLock;.

Reference original text: www.cnblogs.com/-0_0-/artic...

Reference original text: blog.sina.com.cn/s/blog_5cd7...

Reference original text: zhidao.baidu.com/question/14...

Reference original text: ddvcxj.blog.51cto.com/1064441/265...