Java程序通过静态方法检查静态变量的可访问性
静态关键字是Java的非访问修饰符,主要用于内存管理。该静态关键字主要适用于以下情况:
- 变量
- 方法
- 块
- 嵌套类
静态变量是一个声明为静态的变量,意味着在类级别的所有对象之间创建并共享该变量的单个副本。静态方法是一种方法 属于类而不是类的对象,并且可以调用静态方法而无需创建类的实例。
Note:
- The static variable gets memory only once in the class
- A static method can access static data members and can change the value of it.
现在,我们将通过干净的Java程序通过简单的演示来讨论这两种情况,如下所示:
- 案例 1:静态变量对静态方法的可访问性
- 案例 2:静态方法对静态变量的可访问性
案例 1:静态变量对静态方法的可访问性
将变量声明为静态,然后,我们在静态方法中调用该变量,以便将该变量作为输出。
例子:
Java
// Java Program to demonstrate Accessibility of a static
// method/s by static variable/s
// Importing input output classes
import java.io.*;
// Main class
public class GFG {
// Declaring static variable
static int i = 10;
// Main method
public static void main(String[] args)
{
// Print and display the static variable
System.out.println("Static Variable = " + i);
}
}
Java
// Java Program to demonstrate Accessibility of an static
// variable by static method
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Declaring and initializing variables
// Making variables static
static int i = 10;
static int j = 20;
// Declaring the static temporary array
static int temp[] = { 2, 6, 3, 0, 1, 7 };
// Method 1
// Multiplication of array elements
public static void multiply(int n)
{
for (int k = 0; k < n; k++) {
// Multiplying each element of array with i=10
temp[k] = temp[k] * i;
}
}
// Method 2
// To print an array
public static void print_Array(int n)
{
// Display message
System.out.print("\nArray = ");
// Iteration using for loop to print complete array
for (int m = 0; m < n; m++) {
// Printing array element
System.out.print(temp[m] + " ");
}
}
// Method 3
// Main driver method
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.out.print("Static variable : " + i);
// Length of static array temp[]
int n = temp.length;
// Calling the static multiply method
multiply(n);
// Calling the static Print_Array method
print_Array(n);
}
}
输出
Static Variable = 10
案例 2:静态方法对静态变量的可访问性
将变量、数组和方法声明为静态,其中静态变量和静态数组由静态方法访问。在不创建类的实例的情况下调用静态方法。
例子:
Java
// Java Program to demonstrate Accessibility of an static
// variable by static method
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Declaring and initializing variables
// Making variables static
static int i = 10;
static int j = 20;
// Declaring the static temporary array
static int temp[] = { 2, 6, 3, 0, 1, 7 };
// Method 1
// Multiplication of array elements
public static void multiply(int n)
{
for (int k = 0; k < n; k++) {
// Multiplying each element of array with i=10
temp[k] = temp[k] * i;
}
}
// Method 2
// To print an array
public static void print_Array(int n)
{
// Display message
System.out.print("\nArray = ");
// Iteration using for loop to print complete array
for (int m = 0; m < n; m++) {
// Printing array element
System.out.print(temp[m] + " ");
}
}
// Method 3
// Main driver method
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.out.print("Static variable : " + i);
// Length of static array temp[]
int n = temp.length;
// Calling the static multiply method
multiply(n);
// Calling the static Print_Array method
print_Array(n);
}
}
输出
Static variable : 10
Array = 20 60 30 0 10 70