0

i'm new to programming. I'm trying to make a class who start with :

public class Team {

    String teamName;
    String [] player= new String[4];
    Competition [] competitions=new Competition[2];

    public Team (String newTeamnName, String [] newPlayer, Competition [] newCompetition) {
        teamName= newTeamnName;
        player= newPlayer;
        competitions=newCompetition;
    }

but when using the constructor whith this:

public class TeamDemo {

    public static void main (String [] arg){
        Team marc=new ("marcTeam",{"marcello","dfsf","sdfsd"},{"comp1","comp2"})
}

I get an error message about the array {"marcello","dfsf","sdfsd"} and {"comp1","comp2"} that array initializer is not allowed here.

I can't declare a class object with arrays in the parameter ?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Marc We
  • 57
  • 5
  • Team marc=new Team("marcTeam",{"marcello","dfsf","sdfsd"},{"comp1","comp2"}); – Maxim Dec 16 '16 at 14:54
  • I don't understand the answer's, can someone explain, it's the first time i use array – Marc We Dec 16 '16 at 15:10
  • i try the vararg , but it say it have to be the las, but i have 2 array, so it doewsn't work, the rest of the answer i don't understand – Marc We Dec 16 '16 at 15:12
  • 1. In your main you have to write "new Team" since the compiler needs to know what object you are creating. 2. your last argument {'"comp1", "comp2"} is an array of strings. But you wrote in your constructor that you expect an array of Competition. so you would have something like 'new Team("markTeam", {"marcello", "dfsf", "sdfsd"}, {new Competition(?), new Competition(?)});' i dont know your competition-constructor thats why ?. –  Dec 16 '16 at 15:23
  • The answers there say, that `{...}` can only be used why declaring an array variable. You don't do that in your code, so you have to use the general way `new String[]{....}`. And before you wonder `new String[]{"comp1","comp2"}` won't work here, because your code expects `Competition[]`, not `String[]`, so you need to create `Competition` instances instead of using String literals. – Tom Dec 16 '16 at 15:25

0 Answers0