JAVA常见面试题库及解答(10)

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。

本文已影响6827
上一篇:华图面试题目及参考答案 下一篇:软件测试面试题库及参考答案

相关文章推荐

|||||