日期:2014-05-20  浏览次数:20713 次

问一个简单的问题~~~~
初学java,总觉得一些概念很混乱
今天学了一个程序,非常简单
import   javax.swing.JFrame;
public   class   TryWindow   {
      static   JFrame   aWindow   =   new   JFrame( "This   is   the   Window   Title ");  
  public   static   void   main(String[]   args)   {
        int   windowWidth   =   400;                                                            
        int   windowHeight   =   150;                                                          
        aWindow.setBounds(50,100,windowWidth,   windowHeight);            
        aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        aWindow.setVisible(true);                                                  
    }
}
当把aWindow定义在main()外面时要加上static
当我把   static   JFrame   aWindow   =   new   JFrame( "This   is   the   Window   Title ");  
移到main()里面就要把static去掉
这是为什么呢?请高人指点一二


------解决方案--------------------
你把它放到方法(main)里当然是不行啦,不能有static,如果你放到外边,因为main方法是static 所以它只能访问它所在的类的静态字段或者方法