[Looking for Charlie's main web site?]

Java 10 added a var keyword that is quite different from that in CFML

Did you know that Java 10 (and above) has added use of a "var" keyword in variable declaration? Note that it's quite different from CFML's var keyword.

This is NOT something that CF developers NEED to know, and it WILL NOT affect anyone's CFML code.

I just thought I'd share news of it as I happened to learn of it today (though Java 10 came out nearly 5 years ago). More than anything, I thought it worth mentioning for the sake of either CF folks or Java folks who may come to work with CFML, to help each group understand how that Java var keyword is so very different from the one used in ColdFusion, for better or worse.

About the Java var keyword

First, as for this Java var keyword (new since Java 10), it's discussed in detail in the proposal for it here: https://openjdk.org/jeps/286 (and indicated there as closed and delivered in Java 10, which was released in 2018). And of course, it applies then to all subsequent Java versions, including Java 11, the current long-term support (LTS) release that ColdFusion formally supports (we can hope soon that there will be a CF update that adds support for Java 17, the latest LTS Java version out since 2021).

And for those intrigued to know more about this new Java var keyword, see this useful post: https://openjdk.org/projects/amber/guides/lvti-faq.

As both post's indicate, the Java var keyword is a new way to declare a "local variable type inference", with the intent to allow Java devs to no longer have to specify the "type" of a variable (on the left-hand side reference) but let Java infer it from the type of its initializer, if there is one.

If you're a Java dev and this is your first learning of this, you may naturally have a visceral reaction to hearing of this. See the previous two links for more, including acknowledgment of the controversy in this new Java option. To be clear, this post is NOT a place to discuss that. See instead the last link I offer below, which is a place where Java devs could vent.

About the CFML var keyword

By contrast, CF's var keyword is very different: it's simply used to declare a var to be local to a function/method.

CF devs should know by now that without it, a variable (even in a function/method) is presumed instead to be in the variables scope, which can lead to unexpected problems, subtle bugs, and even memory leaks. For more on the var scope in CFML, see first the discussion in the Adobe ColdFusion docs, here.

FWIW, the CFML var keyword was added in CF6 (CFMX), which came out in 2002. And some may want to note that CF9 added the "local scope", as an alternative way to declare a variable to be local to a given function or method.

Still others would want to note the long-available "var scoper" tool which was created originally by Mike Schierberl to help find and rectify occurrences of variables in methods/functions which were NOT locally scoped one way or the other.

Indeed, there is of course a LOT more that can (and has been) said about proper use of locally scoped variables in methods or functions. Again, that's not the point of this post. :-)

Interested readers may want to check out a blog post I did on the topic "some years ago" (and which I updated a couple of years ago for any failing links): The Ultimate Var Scope Resource list? Understanding/resolving problems with the var scope in CFML


So again, my original and main point here is simply to note all this:

  • for the sake Java folks, if they may have experience with this var keyword (since its release nearly 5 years ago), if they may become involved in maintaining/developing CFML code. They could be tripped up if expecting more out of CF's (much older) var keyword
  • for the sake of CF folks, if they may somehow come to notice this Java var keyword and wonder how it differs, or if they may somehow end up in a discussion with Java folks about it

BTW, I want to acknowledge the awesome Java weekly newsletter, whose issue 446 today mentioned the Java var keyword and pointed to that second link above. That started me down this rabbit hole. :-) And at first I started to just mention that post and this difference as a tweet, but I then realized I needed to offer more context, thus this post.

As always, hope that's helpful.

For more content like this from Charlie Arehart: Need more help with problems?
  • If you may prefer direct help, rather than digging around here/elsewhere or via comments, he can help via his online consulting services
  • See that page for more on how he can help a) over the web, safely and securely, b) usually very quickly, c) teaching you along the way, and d) with satisfaction guaranteed
Copyright ©2022 Charlie Arehart
Carehart Logo
BlogCFC was created by Raymond Camden. This blog is running version 5.005.
(Want to validate the html in this page?)

Managed Hosting Services provided by
Managed Dedicated Hosting