0

I'm trying to construct an array of array using Objects. I created the Object Block which contains multiple variables in it. I wrote:

Block[][] block = new Block[4][4];

Now I am having an error when I insert:

block[1][1] = new Block(); // As what I've understood, we still need 
                           // to create the object inside this specific block[][]

The error message is:

Cannot find symbol, ']' expected and Invalid method declaration, return type required

10
  • 9
    Now I am having an error when I insert: what error? Commented Aug 22, 2012 at 7:40
  • 1
    Please post a short but complete program demonstrating the problem. Commented Aug 22, 2012 at 7:40
  • 1
    What's the error message? Noone can help you if you don't provide all the information that you can provide, wether or not it seems important to you. Commented Aug 22, 2012 at 7:41
  • 2
    Does Block() have a parameterless constructor? Commented Aug 22, 2012 at 7:43
  • 2
    To "improve" @hmjd's question: Does Block have visible parameterless constructor? Commented Aug 22, 2012 at 7:46

1 Answer 1

4

It works for me

public class testProgram {

    public static void main(String args[]) {
        Block[][] block = new Block[4][4];
        block[1][1] = new Block();
        block[1][1].setName("Block 1 1");
        System.out.println(block[1][1].getName());

        block[1][2] = new Block();
        block[1][2].setName("Block 1 2");
        System.out.println(block[1][2].getName());
    }

    public static class Block {

        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

I have a question. I tied this, and it's working. I placed this in my Main page. I think I'm noticing something, the error occured when I placed this syntax in another class. Is there a reason for that?
@JonaziAlmocera: Hard to tell without the real code - but if Block is an innder class - it probably has something to do with needing an enclosing instance. Declare Block as public static class Block { ... } instead of public class Block { ... } (Note the difference is the static keyword)
I am not clear what your mean. I test these code in two different classes.
@JonaziAlmocera Go and provide us the error message you get. Then we can tell you what's your problem instead of making blind guesses.
I appreciate your help! (I didn't test yet the public static class Block, but I was thinking to use that) By the way, here is the actual code: In my Main: public static void main(String[] args) { // TODO code application logic here Block[][] block = new Block[4][4]; block[1][1] = new Block(); block[1][2] = new Block(); } and it works perfectly fine.. Continuation... (Sorry, I'm a newbie here at stack overflow)
|

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.