简体   繁体   中英

Invalid method declaration: “return type required” in constructor

My problem is here between public and StudentInfo :

public class Student_Database{
  public StudentInfo(int IDnum,String Name,String Year) {
    this.IDnum = IDnum;
    this.Name = Name;
    this.Year = Year;       
  }
}

It looks like you're trying to create a constructor, but the compiler doesn't see this as one. I'm guessing but likely your constructor name isn't exactly matching your class name. If so, fix this. Make them match -- the class name must exactly match that of the constructor, including spelling and capitalization.

As an aside, you'll want to learn and adhere to Java naming conventions, including giving all variables and parameters names that start with lower-case letters. So your field names should be idNum, name, and year.


Edit
You state that the class declaration is: public class Student_Database{} . If so, then the constructor should look like so:

public Student_Database(int IDnum,String Name,String Year)
{
    this.IDnum = IDnum;
    this.Name = Name;
    this.Year = Year;       
}

Although better would be I think to keep it as StudentInfo, and have it look like:

public class StudentInfo {
  private int idNumber;
  private String name;
  private String year;

  public StudentInfo(int idNumber, String name, String year) {
     this.idNumber = idNumber;
     this.name = name;
     this.year = year;
  }

  // getters, setters, toString, equals override, hashCode override

}

I guess you may want to create a method to set StudentInfo

public void setStudentInfo(int IDnum,String Name,String Year)
{
    this.IDnum = IDnum;
    this.Name = Name;
    this.Year = Year;       
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM