修饰符的非法组合:公共和私有

Cod*_*rry 2 java syntax-error class-visibility

我遇到以下代码的问题...

/**
 * This class holds all of the information pertaining to 
 * a person's name.
 */
public class Name {

    private String first, middle, last, maiden, initials, prefix, suffix;
    private char middleInitial, firstInitial, lastInitial;
    private

    /**
     * Constructor for a Name given first, middle, and last names.
    */
    public Name(String first, String middle, String last) {

        this.first = first;
        this.middle = middle;
        this.last = last;
        this.middleInitial = middle.charAt(0);
        this.firstInitial = first.charAt(0);
        this.lastInitial = last.charAt(0);
        this.initials = String.valueOf(firstInitial + middleInitial 
            + lastInitial);
        this.maiden = null;
        this.prefix = null;
        this.suffix = null;

    }
Run Code Online (Sandbox Code Playgroud)

还有更多,但我的错误是在我的主要构造函数中.它给了我在标题中输入的错误.如您所见,我的类和构造函数都是公共的.这不应该引起任何问题,但似乎正在这样做.

Mur*_*nik 12

private在构造函数的注释之前有一个"孤儿" 修饰符:

private // Here!

/**
 * Constructor for a Name given first, middle, and last names.
 */
public Name(String first, String middle, String last) {
Run Code Online (Sandbox Code Playgroud)

只需删除它,你应该没事.


Aas*_*set 6

private课堂上的第三行有一个迷路.由于语句持续到遇到大括号或分号,编译器认为这是与构造函数声明相同的语句的一部分 - 它看到了private public Name(String first, String middle, String last).