-5

I'm trying to set the string[] of object e1 but I keep getting an illegal start of expression error. I'm new to Java and I have absolutely no idea what is wrong. Any help is appreciated.

public class Employeetest {

    public static void main(String[] args) {

        Employee e1 = new Employee();
        e1.person ={"","","","",""};

        e1.printInfo();
    }
}


public class Employee {

    String[] person ={"","","","",""};

    public void printInfo() {
        System.out.println("Name:" + person[0]);
        System.out.println("Gender:" + person[1]);
        System.out.println("Job Title:" + person[2]);
        System.out.println("Organization:" + person[3]);
        System.out.println("Birthday:" + person[4]);
    }
}
Joe McBride
  • 3,584
  • 2
  • 30
  • 36
ikilltheundead
  • 101
  • 1
  • 3
  • Welcome to Stack Overflow! What specific errors are you getting? What do you think they mean? What have you done so far to try to fix it? The more detail you provide, the better we'll be able to help out. – templatetypedef Jan 21 '15 at 00:01
  • possible duplicate of [Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java) – Marko Gresak Jan 21 '15 at 00:02

1 Answers1

3

When initializing a string array, you don't have to provide new String[], provided that you have initialized the array at the declaration, e.g.

String[] person ={"","","","",""};

Anywhere else you change the value, you must provide new String[] like this:

e1.person = new String[] {"","","","",""};

However, here, you don't need to re-initialize the person array. When you created the Employee object, person was initialized as you have declared it. You can remove this line:

e1.person ={"","","","",""};

and it will work the same. But it won't do much besides printing empty fields unless you initialize the elements of the array to something besides "".

rgettman
  • 167,281
  • 27
  • 248
  • 326