-1

I have two classes, Game and ScoreBoard. I want an instance of ScoreBoard to be generated automatically when I create an instance of Game.

The ScoreBoard constructor looks like this:

public void ScoreBoard(String player1, String player2)
{
    p1Name = player1;
    p2Name = player2;
    p1Score = 0;
    p2Score = 0;
}

and the constructor of the Game class looks like this:

public Game()
{ 
  //irrelevant code redacted 
  ScoreBoard scores = new ScoreBoard(p1, p2);
}

when I try to compile, I get the following message:

cannot find symbol - constructor ScoreBoard(java.lang.String,java.lang.String)

ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79

1 Answers1

5

A constructor doesn't have a return type. Remove the void.

public /* void */ ScoreBoard(String player1, String player2)
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • give u +1 for explaining why? โ€“ Kick Buttowski Jan 15 '15 at 01:39
  • 1
    @KickButtowski that was already asked and answered [here](http://stackoverflow.com/questions/1788312/why-do-constructors-not-return-values) โ€“ fvu Jan 15 '15 at 01:41
  • [JLS-8.8. Constructor Declarations](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8) which says (in part) *The `SimpleTypeName` in the `ConstructorDeclarator` must be the simple name of the class that contains the constructor declaration; otherwise a compile-time error occurs. In all other respects, the constructor declaration looks just like a method declaration that has no result ([ยง8.4.5](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.5)).* โ€“ Elliott Frisch Jan 15 '15 at 01:45