如何通过静态方法检查静态和非静态变量的可访问性?
static 关键字是Java中的非访问修饰符,可用于变量、方法和代码块。 Java中的静态变量属于类,即它只在执行开始时初始化一次。通过使用静态变量,类的所有实例共享一个副本,并且可以通过类名直接访问它们,不需要任何实例。静态方法同样属于类而不属于实例,它只能访问静态变量而不能访问非静态变量。
例1:静态方法可以访问静态变量。
Java
// Java program to check accessibility
// of static variables inside
// static methods
class GFG {
// declaring variable 'a' as static
static int a = 5;
// main is also a static type
public static void main(String args[])
{
// accessing value of
// static variable
System.out.println("Static variable:" + a);
}
}
Java
// Java program to check accessibility
// of non - static variables inside
// static methods
class GFG {
// declaring variable 'a' as non - static
int a = 5;
// main is also a static type
public static void main(String args[])
{
// accessing value of
// non - static variable
System.out.println("Non - Static variable:" + a);
}
}
Java
// Java Program to demonstrate the accessibility
// of static and non-static variables
// by static method
class Student {
// initialized to zero
int a;
// initialized to zero when class is loaded
// but not for each object created.
static int b;
// Constructor
Student()
{
// incrementing static variable b
b++;
}
// method for printing
public void printData()
{
System.out.println("Value of a = " + a);
System.out.println("Value of b = " + b);
}
/*public static void increment(){
a++;
}*/
}
// Driver class
class GFG {
// main is a static block
public static void main(String args[])
{
// creating instance
Student s1 = new Student();
s1.printData();
// directly accessing variable 'b'
// by class name because it is static
Student.b++;
s1.printData();
}
}
输出
Static variable:5
示例 2:我们无法访问静态方法中的非静态变量。
Java
// Java program to check accessibility
// of non - static variables inside
// static methods
class GFG {
// declaring variable 'a' as non - static
int a = 5;
// main is also a static type
public static void main(String args[])
{
// accessing value of
// non - static variable
System.out.println("Non - Static variable:" + a);
}
}
输出
prog.java:16: error: non-static variable a cannot be referenced from a static context
System.out.println("Non - Static variable:" + a);
^
1 error
示例 3:
Java
// Java Program to demonstrate the accessibility
// of static and non-static variables
// by static method
class Student {
// initialized to zero
int a;
// initialized to zero when class is loaded
// but not for each object created.
static int b;
// Constructor
Student()
{
// incrementing static variable b
b++;
}
// method for printing
public void printData()
{
System.out.println("Value of a = " + a);
System.out.println("Value of b = " + b);
}
/*public static void increment(){
a++;
}*/
}
// Driver class
class GFG {
// main is a static block
public static void main(String args[])
{
// creating instance
Student s1 = new Student();
s1.printData();
// directly accessing variable 'b'
// by class name because it is static
Student.b++;
s1.printData();
}
}
输出
Value of a = 0
Value of b = 1
Value of a = 0
Value of b = 2
在上面的程序中,如果我们删除 increment 方法的注释,则会引发错误,因为 increment 方法是静态的,而变量 a 是非静态的,我们已经知道静态方法不能访问非静态变量。