• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays outside of methods.  RSS feed

 
Kyle Fraser
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey! So I'm really new to Java, and I'm learning using Headstart Java. I'm working on a side project, which will eventually hopefully be a Pokedex, and I've just been going to it at the end of every chapter and using the stuff I've learned to work on it.

So I just read chapter 3, which is all about variables and teaches how to use arrays.

my question is, does an array have to be declared inside a method? Because I'm trying to create an array inside a class without any methods and without the main, and I continuously get errors. Here's a quick working of my code that won't compile.




The error message focuses on Telling me the 0 should be a ], the = is an illegal start of type, so on and so forth.

The program compiles completely fine if it's within a method, like this:




Or if I have But I'm trying to practice working outside of main.

So does an array have to be within a method, or am I just daft, and missing something obvious?

Thanks in advance.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all. Do not declare arrays like this:

blah a[]

Better way is:
blah[] a

Also, class names should stard with capitall letters.

Second, you may declare an array as a class member.
If you want to fill it without a method you have some options.

In a constructorIn an initialization blockIn the same statement as declaration of the array. Note that you don't provide a size of array. It is derived from the number of elements you put in the braces.You might as well omit the new Blah[]:
 
Kyle Fraser
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much, Pawel! Turns out the misplacing of the [] was the culprit, but the rest made it a lot clearer, and made way more sense.

Thank you very much!
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle Fraser wrote:Turns out the misplacing of the [] (...)

I would not call it misplacing. Both ways to define an array are perfectly legal as far as the compiler is concerned.
It is just by convention that int[] a is preferred over int a[].
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is that your reference is of type Blah‑array, so you should declare it with the array type: Blah[]. The compiler error was because you are not allowed a statement outside a method.
 
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
Paweł Baczyński wrote:First of all. Do not declare arrays like this:

blah a[]

Better way is:
blah[] a


Admittedly, this doesn't bother me at all. Perhaps, it is because I work with a lot of code, which isn't done by me, and I would go crazy otherwise.

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!