Skip to main content.
November 12th, 2007

Implicit Variables in Java 7?

Few days ago the Belgian Java Users Group (BeJUG) and the Brazilian Java Users Group (SouJava) jointly discussed a submission of few new features as language JSR for JDK7 in the Java Community Process. Lot of small nice features were introduced but one thins was still missing.

Implicit Variable Declarations

The implicit variable declarations with “var” keyword (like in C# 3.0) can significantly reduce the ammount of code developers write every day. For example the code below:

could be simplified to: 

We could use even more interesting constructs like this: 

The type of the “entires” and “entry” variables is implicitly defined from the right side of the expressions in the above code so we don’t want to write the boring and long type definition.

Nice! I am very happy to see this in C# 3.0. Why not in Java 7 too? What do you think?

Posted by nakov in java, blog

This entry was posted on Monday, November 12th, 2007 at 3:12 am and is filed under java, blog. You can follow any responses to this entry through the comments RSS 2.0 feed. Both comments and pings are currently closed.

3 Responses to “Implicit Variables in Java 7?”

  1. shadrik says:

    It sounds very comfortable :), but what about the strict type definition in Java?
    It is interesting to me what is your proposition, is it going to be possible, later, when we finish the work with the concrete object in the var-variable (”entries” in this case), to re-assign to it reference to object of other type (not in the inheritance tree of the previous one, to say, String in our example: entries = “Pesho”;)? Is this going to be allowed?

  2. nakov says:

    Once a variable is defined with the “var” keyword, it takes its type from right side. Later its type can not be changed. The type of var-variables is not java.lang.Object so they cannot take a value of String or Integer.

  3. shadrik says:

    Thank you.