Meowmi
Meowmi

Reputation: 322

Unusual syntax for arrays in Java

I'm doing a school assignment that forces us to use a fixed implementation of the Main class. Inside of the implementation there is a syntax for the declaration of an array (at least it seems like it).

Bank is a class for a Bank object.

Bank.BANKS = new Bank[5];

I have never seen this syntax before and have been looking all over for what it's supposed to mean. My vague understanding of it is that it creates an array (size 5) of Bank objects, but I am not sure about the Bank.BANKS part.

Upvotes: 1

Views: 95

Answers (4)

abhinavsinghvirsen
abhinavsinghvirsen

Reputation: 2034

public class Bank{

    static Bank[] BANKS;
  public static void main(String...arg){
       Bank.BANKS = new Bank[5];
       System.out.println(Bank.BANKS.length);


  }
}

Upvotes: 0

Yibo
Yibo

Reputation: 24

Bank.BANKS is a static variable. It is as same as Bank[] Banks = new Bank[5]

public class Bank {

    public static Bank[] BANKS;

    public Bank() {
        Bank.BANKS = new Bank[5];
    }
}

Upvotes: 0

R G
R G

Reputation: 111

BANKS is a static variable of class Bank. It is a placeholder to store an array of five Bank objects.

Your Bank class might look like this:

public class Bank {
  ....
  public static Bank[] BANKS;
  ....
  ....
}

Upvotes: 0

Mureinik
Mureinik

Reputation: 311573

Bank is the name of the class, and BANKS is a static data member. In other words, if you look at the class declaration, you'll probably see something like this:

public class Bank {
    public static Bank[] BANKS;

Upvotes: 1

Related Questions