java类变量的调用方式

java类变量的调用方式

从类的内部调用类变量,可以使用类名.变量,也可以直接使用变量进行调用:

public class Test {
    static byte a = 1;
    static short b = 2;
​
    public static void main(String[] args) {
        System.out.println("a="+a);
        System.out.println("b="+Test.b);
    }
}

输出结果:

a=1
b=2

而在类的外部调用类变量,只能使用类名.变量

public class Test {
    static byte a = 1;
    static short b = 2;
}
​
class Test2{
    public static void main(String[] args) {
        System.out.println("a="+Test.a);
        System.out.println("b="+Test.b);
    }
}

输出结果:

a=1
b=2

 

posted @ 2021-01-17 20:57  榛子岑  阅读(613)  评论(0)    收藏  举报