0

I am trying to make a program and am having trouble with the syntax. I'm trying to create an array as a parameter for an object, but I am having trouble with the syntax.

public class Example {
    String[] words;

    public Example(words) {
        this.words = words;
    }
}

public class Construction {
    Example arrayExample = new Example({"one", "two", "three"});
}

This gives me an error when I try and compile it. Is there a way to do this without first initializing the array outside the object declaration?

R. Folks
  • 1
  • 1
  • Constructor parameter itself must be declared as `String[] words` and `new String[]{...}` must be used to pass argument. – ernest_k Feb 22 '19 at 17:41
  • 1
    "*This gives me an error when I try and compile it*" - Please include the cmpile error message and highlight the line causing the compile error. – Turing85 Feb 22 '19 at 17:42
  • Change `public Example(words) {this.words = words;}` to `public Example(String[] words) { this.words = words; } ` and everything should be out of the box. Be aware that you also gave package access to `words` attributes of the `Example` class. – Dmytro Chasovskyi Feb 22 '19 at 17:50

3 Answers3

3

Your'e missing the data type of the String array words in parameter of your parametrized constructor. It needs to be String [] words in order to match the data type of your private data member array String[] words. Like this:

public class Example {
    String[] words;

    public Example(String[] words) {
        this.words = words;
    }
}

You can call the constructor from your main without initializing an String[] array like this:

public class Construction {
    Example arrayExample = new Example(new String[]{"one", "two", "three"});
}

What this does is, it instantiates an object at run time and sends it as a parameter directly to the constructor.

abdulwasey20
  • 747
  • 8
  • 21
0

You need to declare the Parameter type of the Constructor Example as below to remove the compilation error in the constructor.

Example(String[] words){
  this.words = words;
}

To pass the array as an argument, you need to either invoke the Array's constructor like this

new Example(new String[]{"I am a string","I am another string"});

or declare it using a variable and use it like this.

String[] argument = {"I am a string","I am another string"};
new Example(argument);

There is a nice explanation in this answer.

0

Didn't see anyone else mention it, but since it appears you're somewhat new to the language, it's also worth mentioning you could use the varargs syntax instead of array for your constructor:

public Example(String... words) {
    this.words = words;
}

This still lets you pass in an array, but also lets you call the constructor with 0 or more plain String arguments:

new Example("no", "need", "to", "pass", "an", "array");
new Example(); // same as empty array and works perfectly fine
new Example("one_word_is_ok_too");
new Example(new String[]{"can","use","arrays","as","well"});

Here's some more background if you're interested.

Mike
  • 4,722
  • 1
  • 27
  • 40