How do I declare a private String without an error

2019-09-21 12:28发布

  public class Seat {

    private int no;
    private String class;
    private boolean pos;
    private boolean table;
    private boolean dir;
    private boolean access;

    public Seat()
    {
    }
}

When I try to declare the String class I continue to get an error, I know it's probably something really simple.

标签: java
4条回答
老娘就宠你
2楼-- · 2019-09-21 12:58

You cannot use class as a variable name because it has a specific meaning in Java. Choose a different variable name for your string.

查看更多
祖国的老花朵
3楼-- · 2019-09-21 12:58

You are using the reserved keyword class to name your variable, which won't compile.

Use a non-reserved name for your String variable.

See variable naming rules here, and reserved keywords here.

The class keyword is used to declare classes, such as in your Seat class declaration.

查看更多
家丑人穷心不美
4楼-- · 2019-09-21 13:09

The error is quite most detected in all the previous answers:

according to the Oracle web site:

Here is a list of keywords in the Java programming language. You cannot use any of the following as identifiers in your programs. The keywords const and goto are reserved, even though they are not currently used. true, false, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.

abstract, continue, for, new, switch assert, default, goto, package, synchronized boolean, do, if, private, this break, double, implements, protected, throw byte, else, import, public, throws case, enum, instanceof, return, transient catch, extends, int, short, try char, final, interface, static, void class, finally, long, strictfp, volatile const, float, native, super, while


So, declaring a variable with any of those names will drive your compiler nuts, and it will complain about the syntax of the code.

查看更多
贪生不怕死
5楼-- · 2019-09-21 13:14

class is a reserved word of java try to rename with other name :

  private String someName;
查看更多
登录 后发表回答