Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #33568
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2016-02-02 13:15 -0800 |
| References | <assignment-20160201130855@ram.dialup.fu-berlin.de> <n8oa1b$tq9$2@dont-email.me> <n8p2a4$8jg$1@gioia.aioe.org> <n8qvt2$qv6$4@dont-email.me> |
| Message-ID | <0d067c12-e281-4e54-928c-4a29742a9a2f@googlegroups.com> (permalink) |
| Subject | Re: Assignment or Initialization? |
| From | Lew <lewbloch@gmail.com> |
On Tuesday, February 2, 2016 at 11:29:25 AM UTC-8, 4ndre4 wrote: > On 02/02/2016 01:55, Arne Vajhøj wrote: > > [...] > >> You can't declare a final variable > >> and THEN assign a value to it, unless you are in a constructor, in which > >> case it's still an initialization. > > > > No. That is not how Java is. > > > > In fact the JLS section about final start with: > > > > "Once a final variable has been assigned, it always contains the same > > value." > > > > so final variables do get assigned (in Java terminology). > > Although you are right on the first point - you can indeed declare a > final variable and then assing a value to it, the question of the OP was > around initialization vs. assignment. I see the first assignment to a > final variable as an initialization, not an assignment. If we consider > the question in those terms, you can't really assign a value to a final > variable, you can only initialize it. > > >so final variables do get assigned > > No, they get initialized. Since the JLS *defines* the Java language, its statements and definitions are normative: <https://docs.oracle.com/javase/specs/jls/se8/html/jls-16.html> "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." Hmm, they call it "assigned". "... every blank final variable must be assigned at most once; it must be definitely unassigned when an assignment to it occurs." There it is again. "For every assignment to a blank final variable, the variable must be definitely unassigned before the assignment, or a compile-time error occurs." (in boldface in the original) They call it "assignment", in boldface no less. "... assignment to the local variable or blank final field ..." "Consider an assignment expression a = b, a += b, a -= b, a *= b, a /= b, a %= b, a <<= b, a >>= b, a >>>= b, a &= b, a |= b, or a ^= b ..." Heck, the whole durn chapter is "Chapter 16. Definite Assignment"! Furthermore, the whole class of "=" operators are called "15.26. Assignment Operators", the effect of each of which is that it "assigns the value" of the right-hand expression to the variable on the left hand. That chapter also informs us that: "A variable that is declared final cannot be assigned to (unless it is definitely unassigned (§16 (Definite Assignment))), ..,." Well, what about what the JLS has to say about initialization? <https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.3.2> "If a declarator in a field declaration has a variable initializer, then the declarator has the semantics of an assignment (§15.26) to the declared variable." and "At run time, the initializer is evaluated and *the assignment* performed exactly once, when the class is initialized (§12.4.2)." [emph. added] "At run time, the initializer is evaluated and *the assignment* performed each time an instance of the class is created (§12.5)." [emph. added] So again, the JLS calls it "assignment", even when it is also an initialization. You should, too. -- Lew
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Assignment or Initialization? 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-01 19:03 +0000
Re: Assignment or Initialization? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-02-01 14:47 -0500
Re: Assignment or Initialization? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-02-01 12:31 -0800
Re: Assignment or Initialization? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-02-01 16:03 -0500
Re: Assignment or Initialization? Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-01 19:52 -0500
Re: Assignment or Initialization? Arne Vajhøj <arne@vajhoej.dk> - 2016-02-01 20:46 -0500
Re: Assignment or Initialization? Wayne <nospam@all.invalid> - 2016-02-01 23:39 -0500
Re: Assignment or Initialization? Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-02 09:14 -0500
Re: Assignment or Initialization? 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-02 19:25 +0000
Re: Assignment or Initialization? Eric Douglas <e.d.programmer@gmail.com> - 2016-02-01 13:13 -0800
Re: Assignment or Initialization? 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-02 19:26 +0000
Re: Assignment or Initialization? Arne Vajhøj <arne@vajhoej.dk> - 2016-02-01 20:55 -0500
Re: Assignment or Initialization? 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-02 19:29 +0000
Re: Assignment or Initialization? Eric Douglas <e.d.programmer@gmail.com> - 2016-02-02 11:44 -0800
Re: Assignment or Initialization? Lew <lewbloch@gmail.com> - 2016-02-02 13:16 -0800
Re: Assignment or Initialization? Lew <lewbloch@gmail.com> - 2016-02-02 13:15 -0800
Re: Assignment or Initialization? Arne Vajhøj <arne@vajhoej.dk> - 2016-02-02 18:13 -0500
Re: Assignment or Initialization? Silvio <silvio@internet.com> - 2016-02-03 12:10 +0100
Re: Assignment or Initialization? Eric Douglas <e.d.programmer@gmail.com> - 2016-02-03 05:14 -0800
Re: Assignment or Initialization? Lew <lewbloch@gmail.com> - 2016-02-03 13:39 -0800
Re: Assignment or Initialization? Lew <lewbloch@gmail.com> - 2016-02-02 10:32 -0800
Re: Assignment or Initialization? 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-02-02 19:22 +0000
Re: Assignment or Initialization? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2016-02-03 09:03 -0800
csiph-web