Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How do classes declare global variables?
How do classes declare global variables?
Sometimes you want to define a class member so that its use is completely independent of any object in the class. Usually, class members must be accessed through the objects of their classes, but you can create such members, which can be used by yourself without referring to a specific instance. You can create such a member by adding the keyword static before the declaration. If a member is declared static, it can be accessed before any object of its class is created, without referring to any object. You can declare both methods and variables as static. The most common example of a static member is main (). Because main () must be called when the program starts executing, it is declared static. A variable declared static is essentially a global variable. When an object is declared, a copy of a static variable is not generated, but all instance variables of this class * * * use the same static variable.

Methods declared static have the following limitations:

They can only call other static methods.

They can only access static data.

They cannot refer to this or super in any way (the keyword super is related to inheritance and will be described in the next chapter).

If you need to initialize your static variables by calculation, you can declare a static block, which will only be executed once when the class is loaded.

The following example shows a class with static methods, some static variables and a static initialization block:

Classes use static {

Static int a = 3;;

Static int b;;

Static void method (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 (strinargs []) {

Methamphetamine (42);

}

}

Once the UseStatic class is loaded, all static statements will run. First set A to 3, then execute a static block (print a message), and finally initialize B to a*4 or 12. Then call main (), and main () calls meth (), passing the value 42 to X. Three println () statements refer to two static variables A and B, and the local variable X.

Note: It is illegal to refer to any instance variable in a static method.

The following is the output of the program:

Static block has been initialized.

x = 42

a = 3

b = 12

Static methods and variables can be used independently of any object outside the class that defines them. In this way, you only need to add a symbol operator after the class name. For example, if you want to call a static method from outside the class, you can use the following general format:

classname.method()

Here, classname is the name of the class that defines the static method. As you can see, this format is similar to the format of calling non-static methods through object reference variables. Static variables can be accessed in the same format-class name plus dot operator. This is how Java implements controlled versions of global functions and global variables.

Here is an example. In main (), the static method callme () and the static variable b are accessed outside their classes.

Class StaticDemo {

Static int a = 42

Static int b = 99

Static void callme() {

system . out . println(" a = "+a);

}

}

Category StaticByName {

Public static void main (strinargs []) {

static demo . call me();

system . out . println(" b = "+static demo . b ");

}

}

The following is the output of the program:

a = 42

b = 99

Static members cannot be accessed by instances created by their classes.

If a member without static decoration is an object member, it belongs to each object.

Members decorated with static are class members, that is, they can be called directly by the class, which is available to all objects.