简体   繁体   English

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

[英]Illegal combination of modifiers: public and private

I am having an issue with the following code... 我遇到以下代码的问题...

/**
 * 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;

    }

There is more but my error is coming in my primary constructor. 还有更多,但我的错误是在我的主要构造函数中。 It is giving me the error that I have entered in the title. 它给了我在标题中输入的错误。 As you can see, both my class and constructor are both public. 如您所见,我的类和构造函数都是公共的。 This shouldn't cause any issues but seems to be doing so. 这不应该引起任何问题,但似乎正在这样做。

You have an "orphan" private modifier before the constructor's comment: 在构造函数的注释之前有一个“孤儿” private修饰符:

private // Here!

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

Just remove it, and you should be fine. 只需删除它,你应该没事。

There is a stray private on the third line inside the class. 班里的第三行有一个流浪private Since statements last until a curly brace or semicolon is encountered, the compiler thinks that this is a part of the same statement as your constructor declaration - it sees private public Name(String first, String middle, String last) . 由于语句持续到遇到大括号或分号,编译器认为这是与构造函数声明相同的语句的一部分 - 它看到private public Name(String first, String middle, String last)

After declaring all your variables, you have written the keyword private . 声明所有变量后,您已将关键字写为private

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

Java is a freely-typed language. Java是一种自由键入的语言。 A line ends with a ; 一条线以a结尾; (semi colon) and not a newline. (半冒号)而不是换行符。 So 所以

private  

public Name(String first, String middle, String last) {
    // ...
}  

is considered to be one single line as: 被认为是一条单行:

private public Name(String first, String middle, String last) {  
  // ...
}  

As you can see, your constructor has two modifiers, public and private . 如您所见,您的构造函数有两个修饰符, publicprivate Which is illegal in Java. 这在Java中是非法的。

Solution

  1. Remove the keyword public if you want to keep the constructor private and DO NOT want other classes to instantiate it. 如果要将构造函数保持为private 且不希望其他类实例化它,请删除关键字public

OR 要么

  1. Remove the keyword private if you want to allow other classes to instantiate it. 如果要允许其他类实例化它,请删除关键字private

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java中的抽象静态类给出错误非法组合修饰符 - abstract static classes in java giving error illegal combination of modifiers 访问修饰符的安全影响(公共,私有,内部,受保护) - Security impact of access modifiers (public, private, internal, protected) Java中表达式的非法开始,同时在公共类中声明私有变量 - illegal start of expression in java while declaring private variables in public class Spring Access修饰符私有 - Spring access modifiers private Java公共/私有和静态修饰符如何影响在一行上声明的多个变量? - How do Java public/private and static modifiers affect multiple variables declared on one line? 使用公共私钥组合通过Java 8进行对称加密 - Using public private key combination for symmetric encryption with Java 8 Java 和 Scala 与私有修饰符的集成 - Java and Scala integration with private modifiers Java 访问修饰符关键字(私有、默认、受保护、公共或最终,static 等)自动隐藏在 eclipse 中,但显示在源文件中 - Java Access Modifiers keyword (private, default, protected, public or final, static etc..) automatically hide in eclipse but show in source file “私人”中的“非法表达” - “Illegal start of expression” in “private” 私有嵌套类中的Java作用域修饰符 - Java scope modifiers in private nested class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM