• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot find symbol class object when trying to initialize an array?  RSS feed

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm trying to make an array of objects, which I then initialize using objects that I have already created. I have main class, a secondary "Other" class, and a third "Other2" class. In the first Other class, I create three objects of the Other2 class, an object array of the type Other2 , and I then try to add the three objects to the object array, which results in the errors:

cannot find symbol: class objectArray, ] expected, identifier expected

here is my code:

Other class


Other2 class


any idea what I'm doing wrong?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Louis Lewis wrote:Hello, I'm trying to make an array of objects, which I then initialize using objects that I have already created. I have main class, a secondary "Other" class, and a third "Other2" class. In the first Other class, I create three objects of the Other2 class, an object array of the type Other2 , and I then try to add the three objects to the object array, which results in the errors:

cannot find symbol: class objectArray, ] expected, identifier expected

here is my code:any idea what I'm doing wrong?


You can't just have java code statements anywhere. Code statements, that is not an declaration, must be in a constructor, initializer, or method.

Henry
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Henry Wong said... In a class you are only allowed to make declarations as its only a template for creating instances of its type...

You can achieve what you want by doing the following:
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see, that makes sense now. Thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!