Solved

How do I declare an array of objects in Java?

Posted on 2007-03-24
6
203 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:mayankeagle
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 30

Accepted Solution

by:
mayankeagle 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:mayankeagle
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 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

18 Experts available now in Live!

Get 1:1 Help Now