Home > Java > javaTutorial > Java program to check accessibility of static method to static variable

Java program to check accessibility of static method to static variable

WBOY
Release: 2023-08-26 17:25:06
forward
998 people have browsed it

Java program to check accessibility of static method to static variable

Introduction

In Java, we can define variables and methods as static. Static variables or methods belong to the class itself, not to individual objects of that class. Therefore, we can access a static variable or method using the class name without creating an object of that class.

In this program, we will explore how to check the accessibility of static variables through static methods. We will define a class with a static variable and a static method to access the variable. Then we will call the static method to check if it can access the static variable.

definition

static

static keyword is very useful in Java to create utility methods or variables that can be accessed from multiple classes without creating an object of that class. It is also used to maintain the same constant value across all instances of a class. However, the use of static variables and methods may bring about potential problems such as thread safety and testing difficulties, so they should be used with caution.

The following are some key features of static in Java -

  • Static variables

  • Static method

  • Static block

  • Static Nested Class

Static Variables - Static variables are class-level variables that are shared among all instances of the class. It is defined using the keyword static and is initialized only once when the class is loaded. Use the class name followed by the dot operator to access static variables.

Case 1: Accessing static methods through static variables

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

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

method

  • Use the "import" statement to import the necessary I/O libraries.

  • Define a Java class named "Tutorialspoint".

  • Declare a static variable "i" of type int and assign it a value of 100.

  • Define the main() method using public, static, void signatures, and take an array of string parameters named "args".

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

  • Save the code in a Java file named "Tutorialspoint.java".

  • Compile Java code using a Java compiler (such as "javac").

  • Run 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);
   }
}
Copy after login

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

The main method of this class is defined using public, static, void signatures and takes an array of string parameters named "args".

In the main method, use the System.out.println() method to print the value of the static variable "i". The output statement contains 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 static variable "i" is set to 100.

Output

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

Static Variable = 100
Copy after login

Case 2: Access static variables through static methods

If you declare a static variable using the public access modifier, any other class can access the variable, including classes containing static methods. In this case, the static method can access the static variable if it is also declared with public access modifier.

However, if a static variable is declared with the 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 the public access modifier.

In this case, we declared a static variable, a static array and a static method in the class. Static methods access both static variables and static arrays, and there is no need to create an instance of the class when calling the method.

Since both static variables and static arrays belong to the class rather than any specific instance of the class, they can be accessed through static methods even if the method is called without creating an instance of the class.

method

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

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

  • 声明一个名为 myArray 的私有静态整数数组,并使用一些值对其进行初始化,例如 myArray = new int[]{1, 2, 3, 4, 5};

  • 声明一个名为 myStaticMethod() 的公共静态方法,它执行以下操作 -

    • 将 count 的值增加 1。

    • 使用 System.out.println() 将 count 的值打印到控制台。

    • 循环遍历 myArray 数组并使用 System.out.println() 将每个元素打印到控制台。

  • 保存 MyClass.java 文件。

示例

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]);
      }
   }
}
Copy after login
  • 创建一个新的 Java 类文件并将其命名为 Main.java。

  • 在 Main.java 文件中,添加 main() 方法。

  • 在 main() 方法中,使用类名称后跟方法名称和括号来调用 MyClass 类的 myStaticMethod() 方法,例如 MyClass.myStaticMethod();。

  • 保存 Main.java 文件。

public class Main {
   public static void main(String[] args) {
      MyClass.myStaticMethod();
   }
}
Copy after login
  • 通过在命令提示符或终端中运行命令 javac MyClass.java Main.java 来编译 MyClass.java 和 Main.java 文件。

  • 通过在命令提示符或终端中运行命令 java Main 来运行程序。

在此示例中,我们有一个 MyClass 类,其中包含静态变量 count 和静态数组 myArray,以及静态方法 myStaticMethod()。 myStaticMethod() 方法将 count 的值加 1,将 count 的值打印到控制台,然后循环遍历 myArray 数组并将每个元素打印到控制台。

输出

在 Main 类中,我们通过使用类名后跟方法名和括号来调用 MyClass 类本身(而不是该类的任何实例)上的 myStaticMethod() 方法。此方法调用将执行 myStaticMethod() 方法并将以下内容输出到控制台 -

Count: 1
1
2
3
4
5
Copy after login

结论

  • 在 Java 中,静态变量和静态方法与类本身相关联,而不是与类的任何特定实例相关联。这意味着静态方法可以直接访问静态变量,只要该变量与该方法在同一个类中定义即可。

The above is the detailed content of Java program to check accessibility of static method to static variable. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template