Solved

How do I declare an array of objects in Java?

Posted on 2007-03-24
6
204 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now