yohhoyの日記

技術的メモをしていきたい日記

Javaのconstキーワード

プログラミング言語Javaでは const をキーワードとして予約しているが、Java SE 7現在でも何ら特定の機能を持たない*1。(恐らく将来に渡って現状のままと予測される)

1999年時点でJDK 1.2.0に対する機能拡張要求 BugID:4211070 Java should support const parameters (like C++) for code maintainence が挙がったが、この要望は 対応意向なし(Won't Fix) としてクローズされた。

It's possible, but I would rather hold the line on creaping featurism. One can design around this (using interfaces, wrappers etc.).
There are no current plans to add this feature to Java. In addition to creeping featurism, we see the following problems with this feature:

  • Adding const is too late now. Had this been added from 1.0, the situation could have been different.
  • Const pollution: the C++ approach requires all const methods to be marked with a const keyword. This means that most methods will have to be marked const explicitly. This tend to clutter all methods in C++.
  • Compatibility is a very important feature of the JDK. Arguably, the collection classes should be modified to indicate that the elements are const. That would require all existing implementations to be updated in the same way, effectively breaking all existing non-JDK implementations of the collection interfaces. Similarly, hashCode would have to be const, breaking the current implementation of String.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4211070

対象問題はコード設計によって回避可能であり、Java言語仕様の拡張は "creeping featurism"*2 であるとしている。

関連URL

*1:同じような予約語に goto がある。wikipedia:en:Java_keywords

*2:日本語では「忍び寄る機能主義」「なしくずしの機能追加主義」などが充てられる模様。いわゆるソフトウェア/システムの肥大化問題。