2

I'm trying to declare the array size of the instance array in a method. Is there any way to do it? Thank you.

package javaapplication2;

public class JavaApplication2 {

static int size;
static int x[] = new int[size];

public static void main(String[] args) {

    setSize(5);
    for (int i = 0; i < 5; i++) {
        x[i] = i;
    }

}

static void setSize(int i) {

    size = i;

}

}

Here's the updated codes. I'm getting an array out of bounds error. I'm assuming because the size did not get declared even like this:

package javaapplication2;

public class JavaApplication2 {

static int x[];

public static void main(String[] args) {

    setSize(5);
    for (int i = 0; i < 5; i++) {
        x[i] = i;
    }

}

static void setSize(int i) {

    x = new int[i];

}

}

I'm getting this error. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at javaapplication2.JavaApplication2.main(JavaApplication2.java:13) Java Result: 1

7
  • perhaps static void setSize(int i) { size = i; x[] = new int[size]; } Commented Mar 19, 2015 at 20:47
  • 1
    Why have you changed your code? Now your question doesn't make sense. Neither any of the answers. Commented Mar 19, 2015 at 20:56
  • 1
    In Java array sizes are not "declared". They are set dynamically at runtime, when the arrays are created. Commented Mar 19, 2015 at 21:02
  • 1
    That updated code will not throw ArrayIndexOutOfBounds. You've issue somewhere else. Commented Mar 19, 2015 at 21:07
  • 1
    @Ski I copy-pasted your code in a fresh class, and it executed successfully without exception. BTW, where is that Java Result: 1 coming from? Commented Mar 19, 2015 at 21:13

3 Answers 3

3

Global variables are initialized before any constructor or the main method, therefore size is declared to 0 by default at the time the array is initialized.

Setting size to 5 won't help changing the already initialized array.

Sign up to request clarification or add additional context in comments.

1 Comment

Of course it is declared. And even initialized to it's default value 0.
1

Initialize the array also in the setSize() method, and remove the initializer from the place of declaration.

static int size;
static int[] x;

static void setSize(int i) {
    size = i;
    x = new int[i];
}

7 Comments

I changed the code above. It still doesn't initialize the size.
What would be the benefit of the size variable, why not just call x.length?
@Ski What is the issue you're facing with the given change?
Or not have it at all. I took it out the code, but someone changed the code but to what it was like before.
@Rohit Jain ArrayOutOfBound error. I'm assuming because the array size is still at 0.
|
1

When your code is executed it runs somewhat like this:

  1. The line static int size declares the variable size and sets its value to 0, which is the default value for int.
  2. When you do static int x[] = new int[size] the value of size is 0. So x[] is initialized as an array of size 0. This all happens before main() gets called.
  3. Later, when you change size value to 5, it has no effect on the size of the x array, since it was already initialized.

You have two options in this case:

  • Set size to 5 when you declare it: static int size = 5,

or

  • Initialize x[] only after calling setSize():

    static int size;
    static int x[];
    
    public static void main(String[] args) {
        setSize(5);
        x = new int[size];
        ...
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.