Java Program to Check the Accessibility of an Static Variable By a Static Method


Introduction

In Java, we can define variables and methods as static. A static variable or method belongs to the class itself rather than to the individual objects of the class. Therefore, we can access a static variable or method using the class name, without creating an object of the class.

In this program, we will explore how to check the accessibility of a static variable by a static method. We will define a class with a static variable and a static method that accesses the variable. We will then call the static method to check if it can access the static variable.

Definition

Static

The static keyword is useful in Java for creating utility methods or variables that can be accessed from multiple classes without creating an object of the class. It is also used for constant values that remain the same across all instances of a class. However, the use of static variables and methods can lead to potential issues such as thread safety and difficulty in testing, so it should be used judiciously.

Here are some of the key features of static in Java −

  • Static Variables

  • Static Methods

  • Static Blocks

  • Static Nested Classes

Static Variables − A static variable is a class-level variable that is shared among all instances of the class. It is defined using the keyword static and is initialized only once, at the time the class is loaded. Static variables are accessed using the class name followed by the dot operator.

Case 1: Accessibility of a Static Method/s by Static Variable/s

If a static method is declared with a public access modifier, it can be accessed by any other class, including the class containing the static variable. In this case, the static variable can be accessed by the static method if it is also declared with a public access modifier.

However, if the static method is declared with a private access modifier, it can only be accessed within the same class. In this case, the static variable cannot be accessed by the static method, even if it is declared with a public access modifier.

Approach

  • Importing the necessary I/O libraries using the "import" statement.

  • Defining a Java class named "Tutorialspoint".

  • Declaring a static variable "i" of type int and assigning it the value 100.

  • Defining the main() method with the signature of public, static, void, and taking an array of String arguments named "args".

  • Inside the main() method, using the System.out.println() method to display the value of the static variable "i" on the console.

  • Saving the code in a Java file with the name "Tutorialspoint.java".

  • Compiling the Java code using a Java compiler such as "javac".

  • Running the Java code using the "java" command, which executes the "main" method and displays the output "Static Variable = 100" on the console.

Example

import java.io.*;

public class Tutorialspoint {

   static int i = 100;

   public static void main(String[] args){
      System.out.println("Static Variable = " + i);
   }
}

In this example, a class named "Tutorialspoint" is defined with a static variable named "i" of integer data type, initialized with the value of 100.

The main method of the class is defined with a signature of public, static, void and takes an array of String arguments named "args".

Inside the main method, the value of the static variable "i" is printed using the System.out.println() method. The output statement includes a string "Static Variable =" concatenated with the value of the static variable "i".

When this program is executed, it will print "Static Variable = 100" as output because the value of the static variable "i" is set to 100.

Output

A static variable is a variable that belongs to a class rather than an instance of the class. This means that only one copy of the static variable is shared by all instances of the class. In other words, static variables are class-level variables that can be accessed without creating an object of the class.

Static Variable = 100

Case 2: Accessibility of a Static Variable by Static Method

If a static variable is declared with public access modifier, it can be accessed by any other class, including the class containing the static method. In this case, the static method can access the static variable if it is also declared with public access modifier.

However, if the static variable is declared with a private access modifier, it can only be accessed within the same class. In this case, the static method cannot access the static variable, even if it is declared with a public access modifier.

In this scenario, we have a static variable, a static array, and a static method declared within a class. The static method accesses both the static variable and the static array, and the method is called without creating an instance of the class.

Since both the static variable and the static array belong to the class and not any particular instance of the class, they can be accessed by the static method even if the method is called without creating an instance of the class.

Approach

  • Create a new Java class file and name it MyClass.java.

  • In the MyClass.java file, declare a private static integer variable called count and initialize it to 0.

  • Declare a private static integer array called myArray and initialize it with some values, for example, myArray = new int[]{1, 2, 3, 4, 5};

  • Declare a public static method called myStaticMethod() that does the following −

    • Increments the value of count by 1.

    • Prints the value of count to the console using System.out.println().

    • Loops through the myArray array and prints each element to the console using System.out.println().

  • Save the MyClass.java file.

Example

public class MyClass {
   private static int count = 0;
   private static int[] myArray = new int[]{1, 2, 3, 4, 5};
       
   public static void myStaticMethod() {
      count++;

      System.out.println("Count: " + count);
              
      for (int i = 0; i < myArray.length; i++) {
         System.out.println(myArray[i]);
      }
   }
}
  • Create a new Java class file and name it Main.java.

  • In the Main.java file, add the main() method.

  • In the main() method, call the myStaticMethod() method on the MyClass class by using the class name followed by the method name and the parentheses, for example, MyClass.myStaticMethod();.

  • Save the Main.java file.

public class Main {
   public static void main(String[] args) {
      MyClass.myStaticMethod();
   }
}
  • Compile both MyClass.java and Main.java files by running the command javac MyClass.java Main.java in the command prompt or terminal.

  • Run the program by running the command java Main in the command prompt or terminal.

In this example, we have a MyClass class with a static variable count and a static array myArray, as well as a static method myStaticMethod(). The myStaticMethod() method increments the value of count by 1, prints the value of count to the console, and then loops through the myArray array and prints each element to the console.

Output

In the Main class, we call the myStaticMethod() method on the MyClass class itself (not on any instance of the class) by using the class name followed by the method name and the parentheses. This method call will execute the myStaticMethod() method and output the following to the console −

Count: 1
1
2
3
4
5

Conclusion

  • In Java, static variables and static methods are associated with the class itself, rather than with any specific instance of the class. This means that static methods can access static variables directly, as long as the variable is defined in the same class as the method.

Updated on: 10-Apr-2023

199 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements