0

I have a class, PriorityTables, for which I have defined an empty default constructor and a constructor that accepts 2 Strings as arguments as seen below.

public class PriorityTable {

   public void PriorityTable(){...}

   public void PriorityTable(String letters, String level){
        PriorityTable(); //Initialize to zero/null, then change values as needed
        ....}
}

PriorityTable pTable = new PriorityTable(s, startExp); //s & startExp being String obj 

There are no errors in that class, and no errors elsewhere in the project before adding that last statement. Yet, when I try to create a PriorityTable object using the 2 String constructor I get the error stating PriorityTable(String,String) is undefined, but I don't understand why. I thought the error might have something to do with calling the default constructor inside the (String, String) constructor, but the error persisted after commenting out that nested default constructor call. Just really weird to be told it's not defined when I can see the definition. Any thoughts?

Matt Waits
  • 33
  • 1
  • 7
  • 2
    `public void PriorityTable(String letters, String level)` -> `public PriorityTable(String letters, String level)` – Eran May 17 '18 at 08:25
  • 6
    A constructor does not have a return type. You did not declare constructors but methods. You now have two methods `PriorityTable`. – Ben May 17 '18 at 08:25
  • And even after correctly declaring the constructor, an invocation from another constructor should be `this()`, shouldn't use the class name. – ernest_k May 17 '18 at 08:27
  • *Face slap* Oh hell. I knew it was going to be a matter of me overlooking something stupid like that. Lol. Thanks everyone. Also, thanks for the heads up about using "this()". I had a funny feeling that was going to be an issue." – Matt Waits May 17 '18 at 15:30

1 Answers1

0

Constructor should not have return type, so please remove the void return type from constructor:

public class PriorityTable {

   public PriorityTable(){...}

   public PriorityTable(String letters, String level){
        PriorityTable(); //Initialize to zero/null, then change values as needed
        ....}
}
Amit
  • 1,540
  • 1
  • 15
  • 28