- Syntax error on token “.”, @ expected after this token

夙愿已清 提交于 2019-12-17 20:58:54

问题


My Eclipse worked fine a couple of days ago before a Windows update. Now I get error messages whenever I'm trying to do anything in Eclipse. Just a simple program as this will display a bunch of error messages:

package lab6;

public class Hellomsg {
    System.out.println("Hello.");

}

These are the errors I receive on the same line as I have my

"System.out.println":
"Multiple markers at this line

- Syntax error, insert ")" to complete MethodDeclaration
- Syntax error on token ".", @ expected after this token
- Syntax error, insert "Identifier (" to complete MethodHeaderName"

回答1:


You can't just have statements floating in the middle of classes in Java. You either need to put them in methods:

package lab6;

public class Hellomsg {
    public void myMethod() {
         System.out.println("Hello.");
    }
}

Or in static blocks:

package lab6;

public class Hellomsg {
    static {
         System.out.println("Hello.");
    }
}



回答2:


You can't have statements outside of initializer blocks or methods.

Try something like this:

public class Hellomsg {
    {
        System.out.println("Hello.");
    }
}

or this

public class Hellomsg {
    public void printMessage(){
        System.out.println("Hello.");
    }
}



回答3:


You have a method call outside of a method which is not possible.

Correct code Looks like:

public class Hellomsg {
  public static void main(String[] args) { 
    System.out.println("Hello.");
    }
}


来源:https://stackoverflow.com/questions/40000269/syntax-error-on-token-expected-after-this-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!