class a

         b hao=new b();  
         public static void main(String [] args)
              {      
                     
                     System.out.println(hao.a);                          
              }
}
class b
{    
    String a=new String("haohao");
    
}
为什么不识别hao啊???

解决方案 »

  1.   

    这样就可以了.
    class   a 
    {   
                           
                      public   static   void   main(String   []   args) 
                                { 
                                   b   hao=new   b();          
                                              
                                              System.out.println(hao.a);                                                     
                                } 

    class   b 
    {         
           String   a=new   String("haohao"); 
            
      

  2.   

    或者可以这样.因为main方法是静态的方法,所以不可以用非静态的变量.
    class   a 
    {   
                      static b   hao=new   b();     
                      public   static   void   main(String   []   args) 
                                {             
                                              
                                              System.out.println(hao.a);                                                     
                                } 

    class   b 
    {         
           String   a=new   String("haohao"); 
            
    }
      

  3.   

    在静态上下文中不能使用外部非静态变量Cannot make a static reference to a non-static field hao
    无法创建一个静态引用给一个非静态域
      

  4.   

    因为main方法是静态的,在静态方法内部只能引用静态属性