Java - Static and Dynamic Array Initialization

52,417

Solution 1

The distinction of dynamic and static allocation is ambiguous (it somewhat depends on the language what it means). In the most general sense, static allocation means that some size has been predetermined, maybe at compile time.

In java, any objects (that includes arrays) are always allocated at runtime. That doesn't necessarily mean it's dynamic, it may still be static in the sense that it can't be changed at runtime. Example:

public class Test1 {
    public final int[] array1 = new int[10];

    public int[] array2 = new int[20];

    public void setArray2Size(int size) {
         array2 = new int[size];
    }
}

The array1 is of size 10, and that can't be changed at runtime. Do note that the final keyword. This lets you assign the "array1" member only once. So you can't assign a different array to this member.

Now, array2 is not final, so you can at any point assign a different array to it, like the setArray2Size()-method does. If there were no assignment after the initial assignment, array2 would still be static in the sense that it can't be changed (because there is no code to do so), although by declaration changing it is allowed.

A concrete instance of an array can not be resized ever once created (there is no language element to express resizing an array in java). It is somewhat hard to grasp for beginners, but a variable like array2 is not the array. It's a reference to the array. You can however replace the reference that array2 holds with the reference to another array, as shown for array2 in setArray2Size()-method.

Solution 2

Memory is not allocated at the time of declaration.

eg: int array[100]; ; -- won't compiles

its only when new operator is encountered memory is allocated.

So

array = new int[100]; ; compiles.

Solution 3

Yes,The size of an array come to know at compile time only but allocation of memory will happen at runtime. Until you encounter 'new' memory won't be allocated to array.

Share:
52,417
Jebeto
Author by

Jebeto

Updated on July 09, 2022

Comments

  • Jebeto
    Jebeto almost 2 years

    Is it true that every array that is initialized during runtime is dynamic and every array that is initialized during compiling is static?

    for example:

    int array[];                 
    public main() {      
        array = new int[100];    
    }
    

    the compiler knows how many elements the array has so it can initilize it during compiling right? or do i need to give every int a value so it becomes static? like this:

    int array[3] { 1, 2, 3};              
    

    and is it posible to define how many elements an array should have outside the main() function? (without giving every int a value) like this:

    int array[100];      
    public main() {
    }
    

    I am programming a little game and it has to run really fast. I read dynamic arrays need a bit longer to process so i want to try it with static arrays, but I am not sure when an array becomes static or dynamic. I searched in many diffrent tutorials but i couldn't find an answer for that.
    thanks for reading.

  • Jebeto
    Jebeto about 11 years
    array = new int[100]; is dynamically right ? but int array[3] { 1, 2, 3}; is static, so is it posible to make a static array without giving the integers a value? by only declaring the size of the array?
  • Suresh Atta
    Suresh Atta about 11 years
    No,you cannot.see [here][stackoverflow.com/questions/1200621/… for valid declarations
  • Jebeto
    Jebeto about 11 years
    thanks for that detailed answer :), now i have one more quick question: array2 is static (array2 = new int[20], without following assignment), but when I initialize array2 like that: array2 = new int[k]; and k is a variable that is defined during runtime, than array 2 is dynamic right? (without following assignment)
  • Durandal
    Durandal about 11 years
    Yes, new int[x] is usually dynamic, but.. a variable "new int[x]" could still be called semantically static if the program can't alter the decision once made (e.g. you read x from a configuration file on start).
  • Jebeto
    Jebeto about 11 years
    ok thanks again, looks like you know a lot about programming, thats awsome :)