0

TestAddress.java

Address[] adrsarr = new Address[5];
        adrsarr[0] = new Address("B402", "3", "42", "Behind Sector 9",
                "New Delhi", "Delhi", "Delhi", "India", "232113");
        adrsarr[1] = new Address("B1", "2", "61", "Bb Road 2", "Mumbai",
                "Mumbai", "Maharashtra", "India", "1213");
        adrsarr[2] = new Address("AH2", "325", "98", "BPGC", "Goa", "Goa",
                "Goa", "India", "403726");
        adrsarr[3] = new Address("a222", "2", "81", "Sector market",
                "New Delhi", "Delhi", "Delhi", "India", "11a001");

Address.java has a constructor of type Address() and of Address(string, string, string, string, string, string, string, string, string)

Now this given code does not work inside the main TestAddress class, it gives an error on the line where I'm declaring adrsarr

- Syntax error on token ";", { expected after this token

But if I put it inside a function like buildArr(), then it compiles flawlessly, no errors.

Any idea what's happening? How am I supposed to initialize an object array without making a function?

5
  • 3
    Please post the whole code as you probally forgot a bracket above the code you posted. Commented Nov 18, 2012 at 10:56
  • Heh, I didn't I've already checked for that. Commented Nov 18, 2012 at 11:03
  • Still helps to post the complete code Commented Nov 18, 2012 at 11:04
  • The code is correct so there must be some other reason for getting the error, maybe it's better to post the complete TestAddress class code. Commented Nov 18, 2012 at 11:29
  • @GanGnaMStYleOverFlowErroR is right, maybe you put your initialization code outside any method and this is not correct. Commented Nov 18, 2012 at 12:02

2 Answers 2

2

Now this given code does not work inside the main TestAddress class, it gives an error on the line where I'm declaring adrsarr

you can't populate your array outside a method,constructor or an initialization block.

 public class Country {
    Address[] add = new Address[3];
    add[0] = new Address();// this would not **compile**, put it inside a constructor /method.

      {
           this.add[0] = new  Address();// populating inside an init block, works fine 
       }
      public country() {
        this.add[0] = new  Address(); //populating inside a constructor, works fine
       }
       public void method(){
         this.add[0] = new Address();//populating inside a method, works fine
        }
  }
Sign up to request clarification or add additional context in comments.

6 Comments

In fact the array is populated in the TestAddres class main method.
@remigio read it propery, i think he meant a class (main TestAddress class), not the main method of the class, but logically speaking, you can populate it inside main.
I've read it carefully, but @user1265125 speaks about using an array of Address in the main function of the TestAddress class, so the comment is not appropriate.
@remigio well, you could be right, but he dint mention main function in the question . (inside the main TestAddress class).
You're right, I interpreted Now this given code does not work inside the main TestAddress class as Now this given code does not work inside the main method of the TestAddress class. Maybe @user1265125 put that code outside any method. I apologize for voting down, I'm just voting up your answer.
|
-1

Use an Array Initializer instead the initializing assignment statements.

Address[] add = { new Address(), new Adress(), new Adress() }; 

Array Initializers can only be used in a declaration or as part of an Array creation Expression (see Java Language Specification - Chapter 10.6 "Array Initializers)

2 Comments

Initializers are not the only way to initialize arrays, you can populate them assigning elements in a subsequent assignment after you have declared them.
Yes you are right, but I did not say that initializers are the only way. I said that initializers can only be used in a declaration or... The way you mentioned is described in the answer above.

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.