总结问题所在:
1. 非静态方法只能用对象或this关键字调用;
2. 静态成员从属于于类,非静态成员从属于对象,
所以在静态方法中调用非静态方法必须用对象调用
3. 静态方法可以相互调用,因为他们不属于某个具体的对象:
改正方式1:
public class Something
{
public static void main(String[] args)
{
Something s = new Something();
System.out.println("s.doSomething() returns " + s.doSomething());
}
public String doSomething()
{
return "Do something ...";
}
}
改正方式2:
public class Something
{
public static void main(String[] args)
{
System.out.println("doSomething() returns " + doSomething());
}
public static String doSomething()
{
return "Do something ...";
}
}
|
|