Solved

How do I declare an array of objects in Java?

Posted on 2007-03-24
6
205 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java 8 lambda expresssions exception handling 3 88
Weekend adv creator 3 36
custom annotations 9 38
null output 3 24
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

786 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