A Slightly Tricky Story About Java Arrays

This article was automatically translated from Japanese by AI. Read the original Japanese version →

Yesterday, while reading some code written by a teammate on-site, I noticed something that bothered me and pointed it out, so I thought I'd share what kind of code it was.
It was pretty funny watching how flustered they got when I asked them to actually fix it.


The code I saw looked roughly like this—with plenty omitted, of course.
In short, the goal was: "Set values into an array variable inside each branch, and then reference the array variable afterward to do something."

private void myMethod(boolean foo){

  // 分岐
  if(foo){
    // 配列変数に値を代入する
    String bar[] = {"月", "Mon" };
		
    // 変数の値を使って何かする。
    System.out.println(bar[0] + "曜日は英語で" +  bar[1] +"です");
  } else {	
    // 配列変数に値を代入する
    String bar[] = {"火", "Tue" };
		
    // 変数の値を使って何かする。 
    System.out.println(bar[0] + "曜日は英語で" +  bar[1] +"です");
  }
}


You can tell at a glance that the "// 変数の値を使って何かする。" (do something with the variable's value) section in the code above is duplicated identically in both branches, which is wasteful.
So I gave the developer the following feedback:


"Please move the '// 変数の値を使って何かする。' (do something with the variable's value) part below the if statement."


Here is what the developer quickly wrote.
Anyone familiar with this will spot the issue right away—it produces a compile error.
Because the scope of the array variable bar ends inside each branch of the if statement, it can't be used outside the branches.

private void myMethod(boolean foo){

  // 分岐
  if(foo){
    // 配列変数に値を代入する
    String bar[] = {"月", "Mon" };
  } else {	
    // 配列変数に値を代入する
    String bar[] = {"火", "Tue" };		
  }

  // 変数の値を使って何かする。 
  System.out.println(bar[0] + "曜日は英語で" +  bar[1] +"です");
}


From here, I walked them through it step by step.
First, to change the scope of the array variable bar, we move its declaration before the if statement.

private void myMethod(boolean foo){

  // 分岐の前に変数を宣言する
  String bar[];

  // 分岐
  if(foo){
    // 配列変数に値を代入する
    bar = {"月", "Mon" };
  } else {	
    // 配列変数に値を代入する
    bar = {"火", "Tue" };		
  }
  // 変数の値を使って何かする。 
  System.out.println(bar[0] + "曜日は英語で" +  bar[1] +"です");
}


At first glance this looks valid, yet it still fails to compile.
The assignments to the array variable inside the branches won't compile properly.


To explain why: in Java, there are only specific moments when you can create an array with an initializer shorthand.

  1. When declaring the array variable
  2. When instantiating the array with new

You can only create an array in those two places.

Therefore, writing a statement like the one below in the middle of your logic will cause a compile error.

bar = {"月", "Mon" };


Unlike primitive types such as int, arrays in Java are treated as objects.
So normally, you can't create one without using new.


And if you ask, "Then why don't you need new when initializing an array variable at declaration time?"—all I can really say is that Java is doing us a special favor there (lol).
(String gets special treatment too, after all.)


So when you want to create and assign an array in Java like this, it looks like:

bar[] = new String[] {"月", "Mon" };

And the corrected version of the earlier program becomes:

private void myMethod(boolean foo){

  // 分岐の前に変数を宣言する
  String bar[];

  // 分岐
  if(foo){
    // 配列変数に値を代入する
    bar = new String[] {"月", "Mon" }; // newを追加
  } else {
    // 配列変数に値を代入する
    bar = new String[] {"火", "Tue" }; // newを追加
  }
  // 変数の値を使って何かする。 
  System.out.println(bar[0] + "曜日は英語で" +  bar[1] +"です");
}


Because Java handles arrays in a rather quirky way, even people with a fair bit of programming experience can trip over this if they haven't learned the exact spec.


And that was a slightly tricky story about Java arrays.