[java] Declaring an array of objects.

Started by
3 comments, last by GameDev.net 19 years, 8 months ago
I've declared an array of objects as such (MAX is declared as 5) Actor ActorList[] = new Actor[MAX]; for (iCount = 0;iCount < MAX;iCount++) { ActorList[iCount] = new Actor (this,41,37); } Then i've got an error when i've tried this line System.out.println(ActorList[3].iLength) The error is "Unresolved symbol ActorList".iLength is a public integer and i've already constructed the object in the for loop.So i can't understand why i still have this error.
Advertisement
In Java, the declaration for arrays is:

Actor[] ActorList = new Actor[MAX];

The only other thing I can think of is that the variable is out of scope when you're trying to print it, or that the Actor class can't be found when you're compiling.
Quote:Original post by aWG
In Java, the declaration for arrays is:

Actor[] ActorList = new Actor[MAX];

No. The square brackets can be either on the type or on the variable. It is not fixed.

Quote:Original post by aWG
The only other thing I can think of is that the variable is out of scope when you're trying to print it, or that the Actor class can't be found when you're compiling.


This is probably the case. You would have to post your full code to see what the problem is.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Quote:Original post by aWG
In Java, the declaration for arrays is:

Actor[] ActorList = new Actor[MAX];


Actually, you can declare arrays the old C-sytle way in Java (though most people don't consider it good practice), so the declaration shouldn't be the problem (but who knows, try it the other way and see). Other than that, it all looks fine. Are you sure you copied all relevent information to the post correctly?

EDIT: Heh, I guess CaptainJester and I were posting about the same time and I corrected something he already had. Sorry! :)
Quote:Original post by Moose6912

Actor ActorList[] = new Actor[MAX];

Then i've got an error when i've tried this line
System.out.println(ActorList[3].iLength)

The error is "Unresolved symbol ActorList".iLength is a public integer and i've already constructed the object in the for loop.So i can't understand why i still have this error.


1. You are supposed to use LOWER CASE first letter for variable names (which is confusing because it can make readers think your error is a class that cannot be found)

2. It's saying it can't find anything with name "ActorList", i.e. the same as if you'd typed:

String anObject = "hello";
System.out.println( anBoject ); // typo in this line, generates "Unresolved symbol"

So, as suggested, the problem is probably that you're trying to access the ActorList var out of scope - e.g. it's only defined in one method, and you tried to call it from another method, where it doesn't physically exist!

redmilamber

This topic is closed to new replies.

Advertisement