?
Solved

How do I declare an array of objects in Java?

Posted on 2007-03-24
6
Medium Priority
?
210 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

Accepted Solution

by:
Mayank S earned 200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

777 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