[1486] in java-interest

home help back first fref pref prev next nref lref last post

Re: Local variables can't be final?

daemon@ATHENA.MIT.EDU (John D. Mitchell)
Tue Sep 5 23:16:10 1995

Date: Tue, 5 Sep 1995 17:01:55 -0700
From: "John D. Mitchell" <johnm@emf2-003.emf.net>
To: prepapol@lifl.fr
Cc: java-interest@java.sun.com, java@java.sun.com, edanuff@protagonist.com
In-Reply-To: <9509051651.AA14601@jenlain.lifl.fr>

prepapol@lifl.fr writes:
> Ed Anuff writes :
>     |-Are local variables allowed to be declared as final?

This is a bit unclear from the Alpha v0.3 draft of Sun's "Java Language
Specification".  There's no direct mention of 'local variables' except for
the distinguishment that they are given in a parenthetical remark in
section 4.2.1 talking about instance variables.  It seems clear though from
the general basis of starting from the C language that this should be
allowed.  Also, from the pseudo-grammar given in Appendix A that the
intention is to allow for this (i.e., 'Statement' is made up of, among
other things, 'VariableDeclaration' which, in turn, allows for the
'Modifiers' preceding the variables).


>     |- The compiler doesn't complain about the following code:
[...Local modification of 'final' local variable...]

Seems to be a bug.


[...]
> I've try your code and look at the doc "java language specification". So
> It seem to me that a final variable must be a constant !

The specification says that directly on page 18, section 4.10.3 of Sun's
Java Language Specification: "Variables marked as *final* behave like
constants".


> When you declare a final variable there is an only memory emplacement
> which is shared by all the instantiations of this class (see "Java
> language environnement" page 26 Part "Static and Final ...").

Hmm... You seem to be confusing the notion of 'static', 'final' and 'static
final'.  When referring to 'variables':  'static' means that the variable
is (in Smalltalkian terms :-) a 'class variable' (i.e., there is only one
actual instance of that variable per *class*); 'final' means that the
variable is an unchanging/constant; and 'static final' means that there is
only a single instance of the variable for the class and it is
unchanging/constant.

In that light, it seems silly to me to ever have a 'final' variable that
is not also 'static'.  Does that mean that 'final' all by itself should
imply 'static'?  It's sort of an odd thing since there isn't (in conceptual
terms anyways) any actual storage for 'constants'.  The translator is
allowed to inline constants or not as it chooses (so in this light, it
seems silly to ever have to say 'static final' at all).  Hmm... :-)


Take care,
	John
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post