• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

initializing a 2d array

 
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

 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Rancher
Posts: 3382
33
  • 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.
 
Sheriff
Posts: 13513
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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: 13513
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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.
 
Marshal
Posts: 64665
225
  • 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.
  •  
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!