Solved

How do I declare an array of objects in Java?

Posted on 2007-03-24
6
207 Views
Last Modified: 2010-03-31
Hello,

Suppose I have a class called Class1, which has a constructor public Class1(int x, int y);

Then I have another class called Class2, in which I want to declare and initialise a two dimensional array containing Class1 objects.

So inside Class2, I would do something like:

private Class1[][] class1Objects = new Class1[10][10]; //Declaring a pointer to a 10 by 10 Class1 array.

/*Initialising all of the objects inside the class1Objects array*/
for(int i = 0; i <= 9; i++)
{
  for(int j = 0; j <= 9; j++)
   {
     class1Objects[i][j] = new Class1(1, 2);
   }
}

I am using Eclipse, and it gives the following error "Syntax error on token ";", { expected after this token" (and I know I'm not missing a "{" anywhere else.)

So clearly I have done something wrong. Can anyone help me correct the mistake please?

Thank you.
0
Comment
Question by:zpivat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 18787215
Which line did you get that error? Could you post the entire code?
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18787347
The code you have posted looks ok, looks like issue is somewhere else. can you post the complete code?
0
 

Author Comment

by:zpivat
ID: 18787393
I see the errors (Eclipse) .on lines 5 and 14

Here you go:

/*Class1.java*/
public class Class1
{
      private int var1;
      private int var2;
      
      public Class1(int x, int y)
      {
            this.var1 = x;
            this.var2 = y;
      }
}


/*Class2.java*/
public class Class2
{
      private int something;
      
      private Class1[][] class1Objects = new Class1[10][10];

      
      for(int i = 0; i <= 9; i++)
      {
        for(int j = 0; j <= 9; j++)
         {
           class1Objects[i][j] = new Class1(1, 2);
         }
      }
      
      public Class2()
      {
            something = 9;
      }
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Accepted Solution

by:
Mayank S earned 50 total points
ID: 18787396
Thought so - there was a method missing. You need to put the for loop in a method:

public class Class2
{
      private int something;
     
      private Class1[][] class1Objects = new Class1[10][10];

      public void initialize ()
      {      
      for(int i = 0; i <= 9; i++)
      {
        for(int j = 0; j <= 9; j++)
         {
           class1Objects[i][j] = new Class1(1, 2);
         }
      }
      }
     
      public Class2()
      {
            something = 9;
      }
}
0
 

Author Comment

by:zpivat
ID: 18787579
Thanks mayankeagle. That seems to solve the problem. Why can't the for loop be outside of a function?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18787594
The logic statements of any Java class have to be in a method. That's what they are for. Depending on the method called, the statements will get executed. You can only have declarations outside the method.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Facing this issue for maven proxy setting 2 27
Adding multiple JVM environments to RedHat 6 7 69
Java Eclipse Loop 3 31
junit as external jar or library 7 39
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question