Java 关于执行语句只能存放在方法体,不能直接在类中编写。Multiple markers at this line - Syntax error on token(s), misplaced c

package com.CSDN;

public class HelloWorld {
    public static void main(String []args){
    	System.out.println("I love U"); 
        int x = 10;        //1
        System.out.println(x);
    }

    class ABC {
    int f = 10;             //2
System.out.println(f);//错误! 因为int f = 10;是执行语句,只能在方法中
    }
}

之所以1处不报错,是因为在main方法里。
而2处报错,是因为类ABC中没有把执行语句"int f = 10;"放到方法中

抛出错误:

Multiple markers at this line
- Syntax error on token(s), misplaced construct(s)
- Syntax error on token “f”, VariableDeclaratorId expected after this
token

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章