Java “私人”中的“非法开始表达”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20307157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:58:28  来源:igfitidea点击:

"Illegal start of expression" in "private"

javaprivate

提问by pabombs

I'd like to know how I could fix the illegal start of expressionerror on line 3

我想知道如何修复第illegal start of expression3 行的错误

1 public class Example {
2     public static void main(String[] args) {
3         private int n;
4     }
5 }

Thanks!

谢谢!

采纳答案by dasblinkenlight

Declarations with scope (i.e. private, protected, or public) must be outsideyour functions, including the main()one. Move these declarations to the class level to fix this syntax error.

具有作用域(即privateprotected、 或public)的声明必须您的函数之外,包括函数main()。将这些声明移动到类级别以修复此语法错误。

回答by AgilePro

The privateis being used in the body of a method. It can not be used there. It can be used in the body of a class ... outside the method.

所述private被在方法的主体使用。它不能在那里使用。它可以用在类的主体中……在方法之外。

回答by Moinul Hossain

private, protected, or publicare access modifiers. They should be used with member variables or methods of a Class.

private, protected, orpublic访问修饰符。它们应该与 a 的成员变量或方法一起使用Class

Class MyClass{
  // private can be used here
  private JFrame window;


  public static void main(String[] args){
     //private can not be used here. It makes no sense
     JFrame localWindow;


  }

}