Static Array in Java
In Java, array is the most important data structure that contains elements of the same type. It stores elements in contiguous memory allocation. There are two types of array i.e. static array and dynamic array. In this section, we will focus only on static array in Java.
An array that is declared with the static keyword is known as static array. It allocates memory at compile-time whose size is fixed. We cannot alter the static array.
If we want an array to be sized based on input from the user, then we cannot use static arrays. In such a case, dynamic arrays allow us to specify the size of an array at run-time.
Static Array Example
For example, int arr creates an array of size 10. It means we can insert only 10 elements; we cannot add a 11th element as the size of Array is fixed.
Advantages of Static Array
Disadvantages of Static Array
Declaring a Static Array
The syntax to declare a static array is:
We can also declare and initialize static array as follows:
Static array can also be declared as a List. For example:
Static Array Java Program
Welcome to Javatpoint
Let's see another Java program.
1 2 3 4 5
Difference Between Static Array and Dynamic Array
The following table describes the key differences between static array and dynamic array.