不懂为什么啊!为什么不能在main()方法体外或者在静态方法外new对象啊???
还有一个不清楚 final修饰局部变量为什么可以不初始化??难道是因为被子类继承时可以让子类赋值?

解决方案 »

  1.   

    public class Test {
     private Object obj = new Object();
    }
      

  2.   

    谁告诉你不能在main()方法体外或者在静态方法外new对象啊???
      

  3.   

    楼主的意思是new自身吧,这个还真不知道
      

  4.   

    都可以,你就是在自己类里面new个自己的也是可以
    public class SingletonTest { public  SingletonTest singleton = new SingletonTest();
    public SingletonTest newObject(){
    return new SingletonTest();
    }
    }
      

  5.   

    可以的。thinkinjave里面有过用例
      

  6.   

    public class TestSuper extends A {
      
     static  TestSuper aa=new TestSuper();       //就是不知为什么去掉static编译没问题
                                                           //,运行就会出错。
     static void f()
     {
       TestSuper aaaaa=new TestSuper();         
      }                                       
      public  TestSuper()
      {
       super("hello");
       
       System.out.println("这里是子类的无参构造方法");
        
      }
      public TestSuper(String s)
      {
       super(s,"parameter2");
       System.out.println("这里是子类的有参构造方法 参数为"+s);  }
      public TestSuper(String s1, String s2)
      {
       System.out.println("这里是子类的双参构造方法 参数s1= "+s1 +"s2 = "+s2);
      }
    //  static TestSuper aa=new TestSuper();
      public static void main(String []args)
      {
       TestSuper test = new TestSuper();        //不能用static修饰方法体内的变量和对象,因为static是属于类的是全局的
       TestSuper test1=new TestSuper("AAA");
       TestSuper test2=new TestSuper("AAAA","BBB");
       f();
      }
    }class A {

      public A()
      {
       System.out.println("这里是父类的无参构造方法");
      }
       public A(String s)
      {
       System.out.println("这里是父类的有参构造方法 参数为"+s);
      }
      public A(String s1, String s2)
      {
       System.out.println("这里是父类的双参构造方法 参数s1= "+s1 +"s2 = "+s2);
      }
    }
      

  7.   

    我这个程序运行时去掉static TestSuper aa=new TestSuper();的static就会运行出错,在main()方法里写TestSuper aa=new TestSuper();又不会错 
    public class TestSuper extends A {
      
     static  TestSuper aa=new TestSuper();    //不能在main方法体外new对象,但static 修饰可以。
     
     static void f()
     {
       TestSuper aaaaa=new TestSuper();      //可以在静态方法内new对象   
      }                                       
      public  TestSuper()
      {
       super("hello");
       
       System.out.println("这里是子类的无参构造方法");
        
      }
      public TestSuper(String s)
      {
       super(s,"parameter2");
       System.out.println("这里是子类的有参构造方法 参数为"+s);  }
      public TestSuper(String s1, String s2)
      {
       System.out.println("这里是子类的双参构造方法 参数s1= "+s1 +"s2 = "+s2);
      }
    //  static TestSuper aa=new TestSuper();
      public static void main(String []args)
      {TestSuper aa=new TestSuper();  
       TestSuper test = new TestSuper();        //不能用static修饰方法体内的变量和对象,因为static是属于类的是全局的
       TestSuper test1=new TestSuper("AAA");
       TestSuper test2=new TestSuper("AAAA","BBB");
       f();
      }
    }class A {

      public A()
      {
       System.out.println("这里是父类的无参构造方法");
      }
       public A(String s)
      {
       System.out.println("这里是父类的有参构造方法 参数为"+s);
      }
      public A(String s1, String s2)
      {
       System.out.println("这里是父类的双参构造方法 参数s1= "+s1 +"s2 = "+s2);
      }
    }
      

  8.   

    我来告诉你吧,这个你得回去好好的理解一下static的含义。
    被static修饰的成员变量和成员方法独立于该类的任何对象。也就是说,它不依赖类特定的实例,被类的所有实例共享。只要这个类被加载,Java虚拟机就能根据类名在运行时数据区的方法区内定找到他们。因此,static对象可以在它的任何对象创建之前访问,无需引用任何对象。
    加上static:在执行main()方法时,jvm虚拟机发现有static TestSuper aa=new TestSuper(); 这条语句,紧接着就去执行 public TestSuper()
      {
      super("hello");
        
      System.out.println("这里是子类的无参构造方法");
        
      }
    这个方法,然后在运行A类里的public A(String s)
      {
      System.out.println("这里是父类的有参构造方法 参数为"+s);
      }
    方法,然后再回头执行main方法里的TestSuper aa=new TestSuper();   这条语句。所有加上static是结果就是导致了main方法里的TestSuper aa=new TestSuper();   在static TestSuper aa=new TestSuper();后执行。不会出现死循环的
    不加static:
    先执行main()里的TestSuper aa=new TestSuper();   然后在去TestSuper类里执行它的构造方法,但是在运行到TestSuper类时,发现又有一条TestSuper aa=new TestSuper(); 语句,这个才去执行TestSuper类public TestSuper()
      {
      super("hello");
        
      System.out.println("这里是子类的无参构造方法");
        
      }
    的构造方法,所以这个时候main()方法里的TestSuper aa=new TestSuper();(执行TestSuper()的构造方法就被挂起了),这个时候在回来执行main()的TestSuper aa=new TestSuper();这条语句.....
    所有这样就又回来了,就成了死循环了,结果内存就溢出了。
    不加static运行程序报的错是内存溢出,程序死循环了.....
    希望你能看明白
      

  9.   

    final  是定义 全局常量的, 所以它不能 初始化的
      

  10.   

    就是说执行main里面的TestSuper aa=new TestSuper()时,在main方法外发现另一个TestSuper aa=new TestSuper(),main方法里的aa就会卡住,而是变成main方法外面的aa去调用无参构造方法。调用后,然后回到main方法体继续执行被卡住的TestSuper aa=new TestSuper(),最后造成死循环????
      

  11.   

    不是,谁告诉你Main()外面不能new一个对象啊。
      

  12.   

    但这个不行啊,这个是一样的啊
    class Test11 {
    public Test11 t1 = new Test11();
            public Test11 newObject(){
            return new Test11();
        }public static void main(String[] args)
      {  }
    }
      

  13.   

    说得好,不加static ,那岂不是会一直递归创建对象了--signature----------------------
    http://www.lunwenwa.com/qklb.htm