public class Something { public static void main(String[] args) { Something s = new

行业问题 面试题库 已帮助 人

public class Something {

public static void main(String[] args) {

Something s = new Something();

System.out.println("s.doSomething() returns " + doSomething());

}

public String doSomething() {

return "Do something ...";

}

}

看上去很完美。

更新中~

答案解析

难度★★☆ 搜索
  • 正确答案:

    错。看上去在main 里call doSomething 没有什么问题,毕竟两个methods 都在同一个

    class 里。但仔细看,main 是static 的。static method 不能直接call non-static methods。可改

    成"System.out.println("s.doSomething() returns " + s.doSomething());"。同理,static method 不

    能访问non-static instant variable。