title: Java Textbook: "the size of an array must be known at compile time"
body: <p>I was just skimming through one of my old textbooks and found this passage defining arrays in Java:</p>
  
  <blockquote>
    <p>A one-dimensional array is a structured composite data type made up of
    a finite, fixedsize collection of ordered homogeneous elements to
    which there is direct access. Finite indicates that there is a last
    element. Fixed size means that the size of the array must be known at
    compile time, but it doesn’t mean that all of the slots in the array
    must contain meaningful values.</p>
  </blockquote>
  
  <p>I have a basic understanding of arrays and am comfortable using them in every day tasks, but I am very confused by the statement that the size of arrays <strong>must</strong> be known at compile time.</p>
  
  <p>A very simple Java program demonstrates that an array can be instantiated with a variable size at runtime:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class test
  {
     public static void main(String[] args)
     {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int size = scan.nextInt();
        int[] array = new int[size];
        System.out.println("You just create an array of size " + array.length);
     }
  }
  </code></pre>
  
  <p>This compiles, executes, and reaches the end without error.</p>
  
  <p>What gives?</p>