Where do we use static variables

8.3 Static methods and member variables

8.3.1 Class variables

Java is a consistently object-oriented language in which there are neither global functions nor global variables. However, since it sometimes makes sense to use properties that are not tied to instances of a class, the language designers introduced the attribute for methods and variables. A variable that has been given the attribute within a class is called Class variable (or Static variable). In contrast to instance variables, which are always linked to a specific object, class variables exist independently of an object.

Each class variable is created only once and can be called by all methods of the class. Since all methods "share" the variable, changes made by one instance are also visible in all other instances. Class variables are therefore comparable to global variables, because their lifespan extends to the entire program. Name collisions cannot occur, however, because external access takes place through qualification with the class name in the form.

A simple example of using class variables is to build an instance counter into a class. Any class variable is introduced for this purpose, which is incremented when an object is created and decremented when it is destroyed. The following example demonstrates this for the class:

001 / * Testauto.java * / 002 003 publicclass Testauto 004 {005 staticprivateint objcnt = 0; 006 007 public test car () 008 {009 ++ objcnt; 010} 011 012 publicvoid finalize () 013 {014 --objcnt; 015} 016 017 publicstaticvoid main (String [] args) 018 {019 test car auto1; 020 test car auto2 = new test car (); 021 System.out.println (022 "Number of test car objects:" + Testauto.objcnt 023); 024} 025}Testauto.java
Listing 8.7: Realization of an instance counter with class variables

The output of the program is: Number of test car objects: 1

An instance of the class was created with, but at the time of the output statement it is only an object reference that has not yet been initialized.

8.3.2 Constants

Another use of class variables is to declare constants. To do this, the attribute is combined with the attribute to create an immutable variable with an unlimited lifespan:

001 publicclass car 002 {003 privatestaticfinaldouble TAX RATE = 18.9; 004}
Listing 8.8: Using Class Variables to Define Constants

The use of prevents the constant CONTROL RATE from being assigned a different value during the execution of the program. Since Java does not contain a preprocessor and therefore does not have an instruction, the method described is the only method for declaring constants in Java. The convention of capitalizing constant names was adopted by C.

8.3.3 Class methods

In addition to class variables, there are also in Java Class methods, i.e. methods that exist independently of a specific instance. They are also called static methods designated. Class methods are also declared with the help of the attribute and - analogous to class variables - are called by placing the class name in front.

Since class methods exist independently of specific instances of their class, access to instance variables is not possible. This separation manifests itself in the fact that class methods have no pointer. Access to instance variables and the call of instance methods is therefore recognized as an error at compile time.

The following listing shows the use of the class method of the class to output a table of square roots:

001 / * Listing0809.java * / 002 003 publicclass Listing0809 004 {005 publicstaticvoid main (String [] args) 006 {007 double x, y; 008 for (x = 0.0; x <= 10.0; x = x + 1.0) {009 y = Math.sqrt (x); 010 System.out.println ("sqrt (" + x + ") =" + y); 011} 012} 013}Listing0809.java
Listing 8.9. Using Math.sqrt

The output of the program is: sqrt (0.0) = 0.0 sqrt (1.0) = 1.0 sqrt (2.0) = 1.4142135623730951 sqrt (3.0) = 1.7320508075688772 sqrt (4.0) = 2.0 sqrt (5.0) = 2.23606797749979 sqrt (6.0) = 2.449489742783178 sqrt ( 7.0) = 2.6457513110645907 sqrt (8.0) = 2.8284271247461903 sqrt (9.0) = 3.0 sqrt (10.0) = 3.1622776601683795

The static method main

A well-known example of a class method is the method that is used as a starting point in applications. In contrast to applets, in which an instance of the applet class is generated by the runtime system and then operated by calling callback methods, an application is started without the instantiation of a class. The Java interpreter only loads the class object specified at the start and looks for a static method with the signature: public static void main (String [] args)

If this is found, it compiles an array with the command line parameters and passes it on as an argument. If no method with this signature is found, there is a runtime error.

8.3.4 Static initializers

So far we have only seen the possibility of assigning a value to static variables during the declaration. If more complex initializations are required, static variables can be used to initialize static initializers To be defined.

They are similar to normal constructors and, like these, are used to assign variables with a defined start value. In contrast to a normal constructor, a static initializer is not called with every newly created object, but only once when the class is loaded.

A static initializer is defined as a parameterless method named:

001 class test 002 {003 staticint i; 004 staticint j; 005 006 static007 {008 i = 5; 009 j = 3 * i; 010} 011}
Listing 8.10: Definition of a Static Initializer

Since the static initializer is called by the runtime system when the class is loaded, user-defined parameterization is not possible.

It is allowed to define several static initializers within a class. They are then executed one after the other in the textual order of their declaration. If initialized static variables and static initializers alternate in the source code, both are treated equally, i.e. the execution takes place in textual order regardless of the type. The only exception are the static variables, to which constants are assigned, the value of which is already fixed at compile time. They are initialized - regardless of their position in the source code - immediately after the class has been loaded.