-1

I was wondering how to create an instance of an object that takes a String[] type as a parameter. So far it comes up with an error saying that I cannot initialise an array when creating a new instance. This is my code:

public class Profile{

    public Profile(String[] interests) {

        this.interests = interests;

    }
}

public class ProfileTest{

    public static void main(String[] args) {

        //Where the error appears
        Profile newProfile = new Profile({"Interest1","Interest2","Interest3"});

    }
}

I don't want to use an arraylist or anything - just String[].

Sian Pike
  • 21
  • 4
  • 1
    this.interests is not a field of your class Profile. You should add a private/public/protected field interests in your class – flox95 Apr 07 '18 at 15:19
  • 1
    Profile newProfile = new Profile(new String[] {"Interest1","Interest2","Interest3"}); – Bentaye Apr 07 '18 at 15:21

1 Answers1

0

To fix the compilation error, use a variable to hold the string array like this:

String[] array = {"Interest1","Interest2","Interest3"};

Profile newProfile = new Profile(array);
EJK
  • 11,784
  • 3
  • 34
  • 53