Static Variable in Java

Static Variable in Java: What is Static Block & Method [Example]

What does it mean for a Java variable to be static?
In Java, a variable is considered to be static if it is both a member of the class to which it belongs and is only ever initialised once at the very beginning of the execution. It is a variable that is considered to be part of the class rather than the object (instance ). The only time that initialization takes place for static variables is at the very beginning of the execution. These variables will be initialised first, before any instance variables are given their own initializations of any kind.

A single copy that will be shared among all instances of the class A variable that is considered static can be accessed directly by the name of the class and does not require any object Syntax:

<class-name>.<variable-name>

What is Static Method in Java?

In Java, a method is considered to be static if it is attached to the class rather than the object to which it is attached. A static method can only access data that is itself static. It is a method that is exclusive to the class and does not belong to any of its objects (instance). A static method can only access data that is itself static. It cannot access non-static data (instance variables).

From within itself, a static method can only call other static methods; it is not possible to call a non-static method from within a static method.
There is no need to pass in an object to call a static method; all that is required is the name of the class.
A static method may not, under any circumstances, make reference to the keywords “this” or “super.”
Syntax:

<class-name>.<method-name>
It is important to note that the main method is a static method because it must be available for an application to run before any instantiation can take place.

By working through some exercises, let’s get a better understanding of the nuances of the static keywords.

Example: How to call static variables & methods

1) Open an editor and paste the code that is below into it.

main is a public static void that belongs to the Demo class (String args[])

Student s1 is created by calling the new Student() function, and then s1.showData() is called. Student s2 is also created by calling the new Student() function, and then s2.showData() is called.

class Student int a; /initialized to zero static int b; /initialized to zero only when the class is loaded, not for each object that is created in this class.

Student(); /Constructor that adds one to the value of the static variable b using b++;

public void showData() System.out.println(“Value of a = “+a); System.out.println(“Value of b = “+b); /public static void increment() /a++; / public void showData() System.out.println(“Value of a = “+a); System.out.println(“Value of b = “+

Save and compile the code as the second step. Execute the code using the java Demo user.

Step 3: The expected results are displayed below.

Methods and Variables That Are Static in Java
The following diagram illustrates how reference variables and objects are created, as well as how different instances access static variables.

Methods and Variables That Are Static in Java
Step 4: You can use the syntax ClassName to access a static variable from outside the class if you want to. This allows you to do so.
Variable Name. Uncomment line # 7 & 8 . Save , Compile & Run . Keep an eye on the results.

The value of an is zero.
1 is the value of b.
The value of an is zero.
Value of b is equal to 2
The value of an is zero.
The value of b is three.
Step 5) Uncomment line 25,26 & 27 . Save , Compile & Run. error: non-static context a cannot be referenced from static context a++; step 6) what does the error mean? This is due to the fact that it is not possible to access the instance variable “a” from within the “increment” method of a Java static class.

What is Static Block in Java?

A Java class can contain a block of statements known as a static block. This block of statements will be run by the Java Virtual Machine (JVM) whenever the class is loaded for the first time. In the same way that constructors contribute to the initialization of instance members, static blocks contribute to the initialization of members of the static data.

Example: How to access static block

class Test static /Code should go here
The following programme serves as an illustration of a static block in Java.

How to gain access to the static block in the public class Demo is an example. a static Integer A, a static Integer B, and a static Integer C. { a = 10; b = 20; }
public static void main (String args[]) {

The output should look something like this: System.out.println(“Value of a = ” + a); System.out.println(“Value of b = ” + b);

The following is the output that you will get from the programme:

The value of an is ten.
The value of b is twenty.