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

在這裏插入圖片描述

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