2017-07-30 120 views
-6

为什么会出现此错误?请参阅下面的代码。为什么这个类不编译?

class Test{ 
    Hello h=new Hello(); 
} 

class Hello{ 
    int a=10; 
    System.out.println(a); // error identifier expected 
} 
+0

你怎么能指望这样即使没有主运行方法? –

+0

很多这方面的错误。没有main()函数。没有任何块的System.out函数。在Test中创建一个main()函数,并将静态后的System out语句置于静态。 – BeardAspirant

回答

0

用同一个包创建类

public class Hello { 

    public void print(){ 

     int a = 10; 
     System.out.println("Number is :" +a); 
    } 
} 

木箱类设置的主要方法在hello方法的同一个包

public class Main { 
    public static void main(String args[]){ 

     Hello h1 = new Hello(); 
     h1.print(); 
    } 
}