0

I am having trouble initializing an array element inside my class. How can I assign an initial value at a specific index of my array field?

public class Account{
    int num[] = new int[50];

    // I can't assign a value like this:    
    num[0] = 12345;
}
Ivar
  • 6,138
  • 12
  • 49
  • 61
Deku
  • 33
  • 8

1 Answers1

2

You can initialize the array in the constructor.

Just write:

public Account(){
    num[0]=12345;
}

inside your class.

The other possibility is to use an initialisation block(but this is less flexible and difficult du document):

{
    num[0]=12345;
}

(Also in your class)

dan1st
  • 12,568
  • 8
  • 34
  • 67