2

Why does this work while the other throws errors:

import java.util.*;

//This works fine
public class ArrayTest {
    public static String[] names = {"James", "John", "Mark"};
    public static void main( String args[] ){
        System.out.print("Names: " + Arrays.toString(names));
    }
}

//But why does this not?
public class ArrayTest {
    public static String[] names = new String[3];
    names[0] = "James";
    names[1] = "John";
    names[2] = "Mark";
    public static void main( String args[] ){
        System.out.print("Names: " + Arrays.toString(names));
    }
}
3
  • 2
    try putting names[0] = "James"; names[1] = "John"; names[2] = "Mark"; inside main. Commented Apr 19, 2011 at 12:40
  • Try putting the names[number] pieces in a static {} block. Commented Apr 19, 2011 at 12:40
  • 1
    You can't have arbitrary statements outside of a method/constructor/initializer. Commented Apr 19, 2011 at 12:41

7 Answers 7

4

Try it like this:

public class ArrayTest {
    public static String[] names;

    static
    {
        names = new String[3];
        names[0] = "James";
        names[1] = "John";
        names[2] = "Mark";
    }

    public static void main( String args[] ){
        System.out.print("Names: " + Arrays.toString(names));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the correction, Stephen C. Greatly appreciated.
1

Enclose the array init inside static init block like follows:

static {
 names[0] = "James";     
 names[1] = "John";     
 names[2] = "Mark"; 
}

1 Comment

I didn't know you could do that.
1

You cannot assign values to the array in the class declaration. You have to do it in a body of a method, e.g. the main method.

public class ArrayTest {

    public static String[] names = new String[3];

    public static void main( String args[] ) {
        names[0] = "James";
        names[1] = "John";
        names[2] = "Mark";
        System.out.print("Names: " + Arrays.toString(names));
    }
}

Or do it in a static block like this:

public class ArrayTest {

    public static String[] names = new String[3];

    static {
        names[0] = "James";
        names[1] = "John";
        names[2] = "Mark";
    }

    public static void main( String args[] ) {        
        System.out.print("Names: " + Arrays.toString(names));
    }
}

Comments

0

Put the initialization in a static block. Else it is run only when a object of that type is instantiated.

Comments

0

The second is invalid syntax. A class declaration can contain only field definitions, method definitions, nested class definitions, and initializer blocks, but not plain statements like names[0] = "James";

Thus, you have to put those statements into a method (which can be called by a field initializer) or an initializer block.

Comments

0

A syntax I like is as follows (esp. for long lists)

public static final String[] names = "James,John,Mark".split(",");

Comments

0

The simple reason :

You cannot execute instructions in the declaration of the class, unless you use a static block such as :

static { names[0] = "James"; names[1] = "John"; names[2] = "Mark"; }

But be aware that a static block is executed once, when the class is loaded by the Java virtual machine.

Therefore, if you create several instances of your class, the static block will still only be executed once.

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.