• Post Reply Bookmark Topic Watch Topic
  • New Topic

declaring multidimensional array  RSS feed

 
Mark Stein
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thinking in arrays in general is enough to make a person's head hurt... but this is driving me crazy...

I've been testing basic code on a JSP page, before putting it into a class (saves on restarting the server)... on my JSP page, I've declared a multidimensional array like this..


It works fine on the JSP page (the page compiles, and I have code that loops through and displays everything. But when I try it in a regular class, I get an error in the line "mixed[0] = a1;" saying an identifier is expected, and it wants a "]".

Yet, I've managed to get the array to compile in the class if I use the following code...


Can anybody tell me what's wrong with the first bit of code (and why it works in JSP but gives netbeans and eclipse fits).

Thx.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got two kinds of Java code lines above: some declarations and some statements. The first three lines of the five -- the ones that declare variables -- are the declarations. The last two are executable statements.

In the body of a Java method, you're allowed to have both declarations and statements. In the body of a class, though, you're allowed only the declarations. So while this is perfectly legal (and is analagous to what the JSP compiler generates



this looks rather similar but won't compile, and indeed will give the message you're seeing:




So I suspect that's what you're doing.
 
Mark Stein
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx... that makes sense. Been so busy looking at the trees, I should have stepped back and looked at the forrest.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!