[英]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
. 如您所见,您的构造函数有两个修饰符, public
和private
。 Which is illegal in Java. 这在Java中是非法的。
public
if you want to keep the constructor private
and DO NOT want other classes to instantiate it. 如果要将构造函数保持为private
且不希望其他类实例化它,请删除关键字public
。 private
if you want to allow other classes to instantiate it. 如果要允许其他类实例化它,请删除关键字private
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.