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?

3
  • Constructor parameter itself must be declared as String[] words and new String[]{...} must be used to pass argument. Commented Feb 22, 2019 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. Commented Feb 22, 2019 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. Commented Feb 22, 2019 at 17:50

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.