2

I'm getting an error in my code (it's the last line of my assignment and I can't seem to solve it) which is in the addStudent method. It says my attempt to access an overloaded constructor is actually attempting to access the default constructor, and that there are no variables for me to pass through. So, in a nutshell:

"studentArray[i] = new Student(firstname, lastname, gender, sClass, sID);"

is trying to access:

"public void Student(){}"

but I want it to access:

"public void Student(String firstname, String lastname, String gender, String sClass, String sID)"

Thanks for the help!

-AndresL

public void Student() 
{
    setFirstName("Unknown");
    setLastName("Unknown");
    setGender("Unknown");
    setClass("Freshman");
    setID("0000");
}

public void Student(String firstname, String lastname, String gender, String sClass, String sID) 
{
    setFirstName(firstname);
    setLastName(lastname);
    setGender(gender);
    setClass(sClass);
    setID(sID);
}

public static boolean addStudent(String firstname, String lastname, String gender, String sClass, String sID)
{
    for (int i=0; i<studentArray.length; i++) 
    {
        if (studentArray[i] == null)
        {
            studentArray[i] = new Student(firstname, lastname, gender, sClass, sID);
            totStudent++;
        }else {JOptionPane.showMessageDialog(null, "Max students reached.");}
    }
    return true;
}
AndresL
  • 80
  • 1
  • 9

5 Answers5

4

You are declaring your "constructors" with void, actually making them void methods, not constructors. Therefore, the compiler can only use the default constructor, which is why you receive that error. So: you should remove the void keyword anywhere you try to define a constructor.

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • When I do so, the methods set_____ have an error saying "overridable method call in constructor" How do I get rid of it? – AndresL Nov 02 '12 at 00:18
  • @AndresL Maybe [this](http://stackoverflow.com/questions/3404301/whats-wrong-with-overridable-method-calls-in-constructors) can help you out here. – arshajii Nov 02 '12 at 00:21
  • Those "errors" are actually warnings, and they come into play when something is extending your `Student` class. If nothing is extending `Student`, then you can either a) ignore them or b) make `Student` final to get rid of them (i.e. `public final class Student { ... }`) – Brian Nov 02 '12 at 00:23
2

Remove void keywords from your constructors:

public void Student(String firstname, String lastname, String gender, String sClass, String sID) 

change to

public Student(String firstname, String lastname, String gender, String sClass, String sID) 

similarly

public void Student() 

change to

public Student() 

Constructors don't have return types.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • When I do so, the methods set_____ have an error saying "overridable method call in constructor" How do I get rid of it? – AndresL Nov 02 '12 at 00:20
1

Constructors don't have a return type because they return a new Object of the same type as the class they are defined in, when you use them with the new keyword.

Object o = new Object(); // you are calling Object constructor.

-- EDIT -- Also I would recommend changing this

public Student() 
{
    setFirstName("Unknown");
    setLastName("Unknown");
    setGender("Unknown");
    setClass("Freshman");
    setID("0000");
}

to

public Student() 
{
    this("Unknown", "Unknown", "Unknown", "Freshman", "0000"); 
}
rizalp1
  • 6,346
  • 2
  • 17
  • 19
0

constructors dont have a return type not even void. you are declaring your constructors with void, which would make them methods rather than constructors

PermGenError
  • 45,977
  • 8
  • 87
  • 106
0

Remove the void from constructors and no need of calling setter methods. You may directly access the attributes as below:

 public void Student() 
 {
    this.firstName = "Unknown";
    this.lastName = "Unknown";
    this.gender = "Unknown";
    this.class = "Freshman";
    this.ID="0000";
 }

Similarly

 public void Student(String firstname, String lastname, 
                                         String gender, String sClass, String sID) 
 {
    this.firstName = firstname;
    this.lastName = lastname;
    this.gender = gender;
    this.class = sClass;
    this.ID=sID;
 }

You should be all set.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73