静态方法里不能直接调用非静态方法,所以可以如下进行修改:
public class Person {
public static void main(String[] args) {
shout();
}
int age;
static void shout(){
int age=60;
System.out.println("my age is " + age);
}
}
如果需要直接调用,那么采用如下方法:
public class Person {
public static void main(String[] args) {
Person person = new Person();
person.shout();
}
int age;
void shout(){
int age=60;
System.out.println("my age is " + age);
}
}
|