Initialize the reference of an array in the constructor? [duplicate]

0
public class Dados{
   int numeros[];

   Dados(){
      numeros = {1,2,3,4,5,6};
   }
}

NetBeans marks me an error. How can I initialize the array in the constructor?

    
asked by Tornes 07.11.2018 в 21:46
source

1 answer

3

That type of initializer can only be used in the declaration of the variable.

int numeros[] = {1,2,3,4,5,6};

If you want to initialize the variable numeros in the constructor you should do it like this:

Dados() {
    numeros = new int[]{1,2,3,4,5,6};
}
    
answered by 07.11.2018 / 21:54
source