0

How can I and what is the best way of initialize the variables using a constructor.

For example I am trying to create a array to hold 6 Strings

class Dice{
        public String [] side = new String[5]; 
        public String name;

        public Dice (String n, String [] a){
            name = n;
            side = a;
        }
    }

I have tried to set values like so:

class diceGame{
    // calling main method
    public static void main (String[] args){
        Dice easy = new Dice("Green:",["Brain","Brain","Brain","a","a","a"]);
}

Dice easy = new Dice("Green:","Brain","Brain","Brain","a","a","a");

Dice easy = new Dice("Green:",{"Brain","Brain","Brain","a","a","a"});

In 2 occasions I get the same

error message error: illegal start of expression

and in the other

error: constructor Dice in class Dice cannot be applied to given types;

found: String,String,String,String,String,String,String

I would like to know which is the most efficient way of doing this?

Here is a previous question which made me think if there are so many ways of doing this which is actually the best?

How to initialize array in java when the class constructor has parameters?

Liam
  • 568
  • 2
  • 15
  • 3
    `Dice easy = new Dice("Green:",new String[]{"Brain","Brain","Brain","a","a","a"});` also `public String [] side = new String[5];` should just be `public String [] side;` – Elliott Frisch Sep 09 '19 at 00:16

1 Answers1

2

Use a varargs constructor:

class Dice {
    public String [] side; 
    public String name;

    public Dice (String n, String ... a){
        name = n;
        side = a;
    }
}

Then this should work:

Dice easy = new Dice("Green:","Brain","Brain","Brain","a","a","a");

If necessary you can add a check to ensure the length of the argument is a specific value, but that is awkward because callers won't know how many args are required. If the array is always a fixed length, you could provide each element as a separate argument and create the array in the constructor body:

class Dice {
    public String [] side; 
    public String name;

    public Dice (String n, String a1, String a2, String a3, String a4, String a5, String a6){
        name = n;
        side = new String[] {a1,a2,a3,a4,a5,a6};
    }
}

This is only suitable for short arrays. Otherwise just take an array argument in the constructor - like the varargs method, but in the javadoc specify that required length and do a validation of the length in the constructor.

swpalmer
  • 3,890
  • 2
  • 23
  • 31
  • Something to note when passing an array to a constructor or any method - the array is passed by reference, so the caller is able to change the elements of the array after the call. If you need to be sure this can't happen, you must make a copy of the array rather than just assigning it to a member variable like I did in the varargs constructor example. – swpalmer Sep 09 '19 at 00:44
  • "the array is passed by reference" - No, it's not. See: [Is Java “pass-by-reference” or “pass-by-value”?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Jacob G. Sep 09 '19 at 00:58
  • Thanks to all this was very informative, I like the spread in JS I did not know that java had one(Varargs method) which is similar. :) – Liam Sep 09 '19 at 23:16
  • 1
    @JacobG. Yes, technically the "array reference" is passed by value. The point is the array object, or ANY object in Java is not passed by value because you are always dealing with object references. – swpalmer Sep 10 '19 at 00:01
  • @JacobG, is it possible to use the Vargas method with ArrayLists? – Liam Sep 10 '19 at 17:04
  • I should also have pointed out that when making a new copy of an array you should always use Arrays.copyOf. There are JVM optimizations for that method that allows the JVM to avoid zeroing out the elements of a new array (as per the language spec) before filling them in with the contents of another. – swpalmer Jul 20 '20 at 03:33