package com.bjsxt.com;import java.util.Scanner;
public class Test{
public static void main(String []args){
Run run=new Run();
run.method1();
}
}
class People{
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void getMessage(){
System.out.println("The name of people is:"+name);
}
}
class Run{
private static Run run=new Run();
private People people=new People();
private static Scanner sc=new Scanner(System.in);
public void method1(){
run.method2();
System.out.println("in method1:");
people.getMessage();
}
public void method2(){
run.method3();
}
public void method3(){
System.out.println("Please input the name of people");
people.setName(sc.nextLine());
System.out.println("in method3:");
people.getMessage();
}
}

解决方案 »

  1.   

    public class Test4{
    static Test4 t=new Test4();//为什么加上static就能正确运行 如果把static去掉运行会报错 这是为什么啊?
    public Test4(){
      
    }
    public static void main(String []args){ 
    Test4 t1=new Test4();

    }我加了注释的那一句谁能给我解释解释啊? 
      

  2.   

    去掉static, t 就是 实例的成员,也就是会在类Test4在实例化时被初始化。而你这个东西又恰好是new一个自己,显然就是个死循环嘛
      

  3.   

    static是和类相关的,并且main方法是一个静态方法,只能调用静态属性;
    静态(static)和非静态的区别:静态是和类有关的,在类加载的时候产生,而非静态是和对象有关的,对象产生的时候非静态属性才产生,非静态方法能调用静态方法和静态属性。而静态方法只能调用静态方法和静态属性。
      

  4.   

    是不是Test4 t=new Test4();中的t没有被附上值,他的构造方法就不往下执行?我Debug运行是他执行到构造方法时,他不往下执行(不会执行到构造方法中),而是又跳到Test4 t=new Test4();这了。不知道为什么。