• Post Reply Bookmark Topic Watch Topic
  • New Topic

initializing a 2d array  RSS feed

 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok for some reason when i try to initialize the array in the testMsg i get an error





main class

 
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
Sean Paulson wrote:ok for some reason when i try to initialize the array in the testMsg i get an error


Java statements, that are not declarations, must be in a constructor, method, or initializer. You can't just have Java statements anywhere.

Henry
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays can be declared and given values in a single statement:
Note: the third row has 3 elements vs only 2 for the other rows.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wrote:

The Java idiom is this:

Writing idiomatically generally makes your code more readable to people who are more experienced in the language. Your version, while not incorrect, will make most people do a double take. Kind of like messing up an expression, like saying "Make a tree and go!" instead of "Make like a tree and leave!" People will still know what you mean but the way you said it is not quite what people are used to.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative fix is to use an instance initializer block. This is not commonly used and you're likely to cause people to do another double take:

It's also very verbose. The way Norm showed earlier is the more common practice. 

There's yet another way which is used even more rarely, at least in my experience.

You can read more about initializers here: https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

EDIT: I guess "must" really is "should" -- you won't get any compile-time errors if it isn't final but making it so can protect you from introducing problems later if the code is modified and extended.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . . The way Norm showed earlier is the more common practice.   . . . .
That construct is called an array initialiser. It has two other advantages not yet mentioned:-
  • 1: At no time is the array ever left without all its elements filled with real values. No nulls ever.
  • 2: The size of the array and its constituent arrays will be exactly right for the number of objects to contain. No risk of out of bounds exceptions while populating the array.
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!