Syntax support for collections?

It is common to use final static fields to define constants in your Java applications. For example, you will often find the code similar to this in various Java APIs

public class Example {
  public static int one = 1;
}

which is used in the following manner

System.out.println(Example.one);

Of course, Java supports more complex initialization of static properties through static initializer blocks. One of the most common usages for these blocks is initialization of static collections with known values.

Take a look at the following code for example:

public class Example {
       public final static HashMap constants = new HashMap();
       static {
		constants.put("1", "First");
		constants.put("2", "Second");
		constants.put("3", "Third");
	}
}

and the usage of such static collection:

System.out.println(Example.constants.get("1"));

Wouldn’t you prefer to have more “native” support in Java for dealing with collections? For example, with the syntax that is found in most “dynamic” languages today, the previous example could be replaced with something like this:

public final static HashMap constants = new HashMap() {
      "1" : "First",
      "2" : "Second",
      "3" : "Third"
    }

Collections are one of the crucial aspects of programming languages and would it be normal to see them embedded in the language syntax? Would this “dynamic” syntax, used in Ruby, Groovy and other dynamic languages, make Java applications easier to write (and read)?

Do you have other syntax enchacements that you would like to see in the following editions of Java?

1 comment

Leave a comment

Your email address will not be published. Required fields are marked *