Last Updated:

Static modifier in java

In Java and any object-oriented programming language, the static modifier in a class sets the scope of fields, constants, and methods within a class, but not its objects. In addition to fields and methods, a static initializer can be declared in a class that specifies an initial value for the fields when the first object is created through the constructor.

What static is used for?

 

As you know, a class is a template for creating objects that copy all its fields and methods. Those fields and methods that have a static modifier are not instances of the objects being created, but of the class itself. When you call a static method, the class name is placed at the beginning instead of referencing the object.

Static elements carry general information about the class itself. The main executing method main also has a static modifier and runs automatically when the program is executed. Without this modifier, to run the program, the compiler would have to create a main class object. Instead, it runs main as the class.

public class newApp {

public static void Main(String[] args){

StatClassobj = new StatClass(5, Hello);
System.out.println(obj.v); \\ Error, it is impossible to call a static method through an object, you need to call through the -> class exit
system.out.println(StatClass.v); \\ Display 5

}
}

classStatClass{
publicint x;
public String s;
public static int v = 100; \\ Static field
public void fun(){
System.out.println("call method of object");
}
public static void statFun(){ \\ Static method
System.out.println("call static method of class");
}
StatClass(int a, String b){
this.x = a;
this.s = b;

}

}

In the example above, a static field and a method were created. If you try to access field v through the generated object, the compiler detects the error and stops working. To access a static field, you should do so through the class: StatClass.v. You should also call its static method: StatClass.statFun().

Each class object that you create will receive new field values. Each object has its own public fields and methods. When static fields will always retain their value exclusively for the class. However, you cannot access a static element using the this statement reference.

Static constants

 

Constants are subject static fields, only their value cannot be changed. Like all static class members, constants are accessed through the class name rather than the object name. Changing the value of a constant will result in a compilation error.

Example:

publicclassApp{

public static void main(String[] args){
System.out.println(Const.cst); \\ 5
System.out.println(Const.cst+5); \\ Error, aborting compilation
}
}
classConst{
public static intcst = 5;

}

Static initializers

A static initializer is an unnamed block in a class with a static modifier. It performs 2 tasks: initializes static fields, methods and constants; executes the statements written to it when the very first object in the class was created. That is, when you call the new constructor, the initializer statements are executed and then an object is created.

The static initializer is called only before the first object is created—there is no other way.

Example:

classApp{
public static void Main(String[] args){
Something obj = new Something(5);
Something obj_1 = new Something(10);
}
}
class Something{
public static int number;

static{ \\ Static initializer
number = 10;
System.out.println(number);
}
Something(int x){
number +=x;
System.out.println(number);
}
}

The result of running the program in the console:

10
15
25

As you can see, the static initializer worked and displayed the first value of the number variable. Then the first constructor worked and increased the number by 5, and the second, adding to the variable 10. The key point here is the first output of the number variable - this is that the static initializer worked automatically, before the first obj constructor fires.

Why use static variables?

 

This can be explained by an example from life. Let the class create document boxes. Each box contains its own number of documents – these are public variables. The static variable will contain information about the total number of mailboxes created. Here, static data is abstract, describing production processes.

Public variables are responsible for the parameters of each individual mailbox, so they can be accessed directly through the mailbox. However, to find out the statistics, you will have to turn to the class itself, which is the manufacturer of these very boxes.

Static variables can be public, secure, or private. A variable with a single static modifier automatically becomes private.

The advantage of such fields and methods is the convenience of organizing the code. They provide flexible options for class description and object manipulation. If there are no parameters in the constructors of an object that specify the public fields—the properties of the object—the static fields remain unchanged. Once again, a static variable is not a property of the instance, but of the entire class, as are static methods that make changes either to the properties of all objects or within the class.

Conclusion

 

Static fields, constants, and methods are separate, abstract constructs of a class. They are used to describe general information regarding the production of objects and the manipulation of them. Static fields can act as counters and summaries. Static methods process a variety of data for a class and all objects. Static elements are accessed through a class reference, but not through a single object.