Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么这个类不编译?_Java_Class_Variables_Scope - Fatal编程技术网

Java 为什么这个类不编译?

Java 为什么这个类不编译?,java,class,variables,scope,Java,Class,Variables,Scope,为什么会出现这种错误?请参阅以下代码 class Test{ Hello h=new Hello(); } class Hello{ int a=10; System.out.println(a); // error identifier expected } 使用相同的包创建类 public class Hello { public void print(){ int a = 10; System.out.println("Num

为什么会出现这种错误?请参阅以下代码

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

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

使用相同的包创建类

public class Hello {

    public void print(){

        int a = 10;
        System.out.println("Number is :" +a);
    }
}
Crate类在Hello方法的同一包中设置主方法

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

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

你怎么能期望它在没有主方法的情况下运行呢。没有主要功能。没有任何块的System.out函数。在测试中生成一个主要函数,并在生成一个静态后以静态方式将系统输出语句。