静态变量可以通过类和实例操作

/**
 * @author 欢迎加入Java技术交流群:646766275
 *
 */
public class Test {

    private static int x = 100;

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Test test1 = new Test();
        test1.x++;

        Test test2 = new Test();
        test2.x++;

        test1 = new Test();
        test1.x++;

        Test.x--;

        System.out.println(x);
    }

}

A. 程序通过编译,输出:103
B. 23行不能通过编译,因为x是私有静态变量
C. 15行不能通过编译,因为引用了私有静态变量
D. 程序通过编译,输出:102

正确答案:D

猜你喜欢

转载自blog.csdn.net/huayushuangfei/article/details/80172706