首页 > 试题广场 >

给定Java代码如下所示,则编译运行后,输出结果是( )。

[单选题]
给定Java代码如下所示,则编译运行后,输出结果是( )。
public class Test {
    static int a;
    int b;
    static int c;

    public int aMethod() {
        a++;
        return a;
    }

    public int bMethod() {
        b++;
        return b;
    }

    public static int cMethod() {
        c++;
        return c;
    }

    public static void main(String args[]) {
        Test test1 = new Test();
        test1.aMethod();
        System.out.println(test1.aMethod());
        Test test2 = new Test();
        test2.bMethod();
        System.out.println(test2.bMethod());
        Test test3 = new Test();
        test3.cMethod();
        System.out.println(test3.cMethod());
    }
}
  • 1 0 2
  • 2 1 2
  • 0 0 2
  • 2 2 2
初始值都是0吗
发表于 2020-05-19 11:11:08 回复(0)
创建对象后执行一次++,输出前一次++,因此输出2
发表于 2018-11-29 18:01:20 回复(0)
静态方法中不能访问非静态的成员方法和非静态的成员变量,但是在非静态的成员方法中是可以访问静态成员方法/变量的。
发表于 2018-11-29 13:47:09 回复(0)