Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #23669 > unrolled thread

Re: A small exercise

Started byDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
First post2013-04-27 15:40 +0200
Last post2013-04-27 15:40 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: A small exercise Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-04-27 15:40 +0200

#23669 — Re: A small exercise

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2013-04-27 15:40 +0200
SubjectRe: A small exercise
Message-ID<klgka1$ecv$1@dont-email.me>
On 20/04/2013 15:11, Stefan Ram allegedly wrote:
>   Try to predict the behavior of the execution or compilation
>   of the following code!
> 
> public class Main
> { final int a; final int b;
>   Main(){ this.a = 0; Main.this.b = 0; }
>   public static void Main( final java.lang.String[] args )
>   { new Main(); }}
> 
>   (I made this observation while programming something else
>   using Oracle JDK 7, but I have not read the JLS about it yet.)
> 

It is weird indeed that the compilation should yield the following error:
  Main.java:3: error: cannot assign a value to final variable b
  Main(){ this.a = 0; Main.this.b = 0; }
                               ^
  1 error

In section 15.11.1., /Field Access Using a Primary/
(<http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.11>),
the JLS states:
  "At run time, the result of the field access expression is computed as
follows:
   (...)
   If the field is not static:
    (...)
   If the field is not final, or is a blank final and the field access
occurs in a constructor, then the result is a variable, namely the named
member field in type T found in the object referenced by the value of
the Primary."

So it should be all right.

Then again, perhaps the following plays a role; the same section also
states:
	"At run time, the result of the field access expression is computed as
follows: (assuming that the program is correct with respect to definite
assignment analysis, i.e. every blank final variable is definitely
assigned before access)" (note the bit in parentheses).

Section 16, /Definite Assignment/
(<http://docs.oracle.com/javase/specs/jls/se7/html/jls-16.html>), states:
	"Each local variable (§14.4) and every blank final field (§4.12.4,
§8.3.1.2) must have a definitely assigned value when any access of its
value occurs.
(...)
	Such an assignment is defined to occur if and only if either the simple
name of the variable (or, for a field, its simple name qualified by
this) occurs on the left hand side of an assignment operator."

Note how assignment with a qualified this isn't mentioned.

Seems like a bit of a grey area to me.

-- 
DF.

[toc] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web