How to access static variable in Java

Darcy 139 Published: 07/24/2024

How to access static variable in Java

In Java, a static variable is shared by all instances of the class and retains its value between method calls. You can access a static variable directly using the class name or an instance of the class. Here are some ways to access static variables:

Using Class Name: You can access a static variable directly using the class name, as follows:
MyClass.myStaticVariable;

Using Instance of the Class: You can also access a static variable using an instance of the class:
MyClass myObject = new MyClass();

myObject.myStaticVariable;

Within a Method of the Same Class: If you want to access a static variable within a method of the same class, you don't need to create an instance of the class:
public void myMethod() {

System.out.println(MyClass.myStaticVariable);

}

Using Reflection: You can also access a static variable using reflection:
MyClass.class.getField("myStaticVariable");

Here is an example program that demonstrates how to access a static variable in Java:

public class MyClass {

public static int myStaticVariable = 10;

public static void printStaticVariable() {

System.out.println("Value of myStaticVariable: " + myStaticVariable);

}

}

public class Main {

public static void main(String[] args) {

// Accessing the static variable using class name

System.out.println("Initial value of myStaticVariable: " + MyClass.myStaticVariable);

// Accessing the static variable using an instance of the class

MyClass myObject = new MyClass();

System.out.println("Value of myStaticVariable through object: " + myObject.myStaticVariable);

// Modifying the static variable within a method of the same class

MyClass.printStaticVariable();

// Modifying the static variable directly

MyClass.myStaticVariable = 20;

System.out.println("New value of myStaticVariable: " + MyClass.myStaticVariable);

}

}

In this program, we define a static variable myStaticVariable within the MyClass class. We then demonstrate how to access and modify this static variable using different methods.

The output of the program will be:

Initial value of myStaticVariable: 10

Value of myStaticVariable through object: 10

Value of myStaticVariable: 10

New value of myStaticVariable: 20

What is static in Java with an example?

I'd be happy to explain what "static" means in Java.

In Java, the "static" keyword has several different meanings depending on its context. One of the most common uses of "static" is to declare a static variable or method that belongs to a class rather than an instance of the class.

Static Variables

When you declare a variable as static, it means that the variable is shared by all instances of the class, and it is initialized only once when the class is first loaded. A static variable is essentially a global variable that is associated with the class instead of an instance of the class.

Here's an example:

public class MyClass {

public static int myStaticVariable = 10;

}

public class Main {

public static void main(String[] args) {

System.out.println(MyClass.myStaticVariable); // prints 10

MyClass obj1 = new MyClass();

MyClass obj2 = new MyClass();

System.out.println(obj1.myStaticVariable); // still prints 10

System.out.println(obj2.myStaticVariable); // still prints 10

}

}

In this example, myStaticVariable is declared as a static variable in the MyClass class. This means that it can be accessed directly without creating an instance of the class. Both obj1 and obj2 have access to the same static variable.

Static Methods

When you declare a method as static, it means that the method is associated with the class rather than an instance of the class. A static method can be called without creating an instance of the class, and it cannot access any non-static variables or methods of the class.

Here's another example:

public class MyClass {

public static void printHello() {

System.out.println("Hello!");

}

}

public class Main {

public static void main(String[] args) {

MyClass.printHello(); // prints "Hello!"

MyClass obj1 = new MyClass();

MyClass.printHello(); // still prints "Hello!"

}

}

In this example, printHello() is declared as a static method in the MyClass class. This means that it can be called directly without creating an instance of the class. Both obj1 and the class itself can call the same static method.

Other uses of "static"

The static keyword has other meanings in Java, such as:

Declaring a block of code to be executed only once when the class is first loaded. Declaring a nested class that is associated with its enclosing class instead of being an inner class. Declaring a lambda expression or method reference that can be used multiple times without creating a new instance each time.

In summary, in Java, "static" refers to variables and methods that are shared by all instances of a class, rather than being unique to individual objects. Understanding the concept of static is crucial for writing robust and efficient Java code.