Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: Assignment or Initialization? Date: Mon, 1 Feb 2016 20:55:18 -0500 Organization: Aioe.org NNTP Server Lines: 42 Message-ID: References: NNTP-Posting-Host: ANasIS7Js9OyLQwgloeBLQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:33558 On 2/1/2016 2:03 PM, 4ndre4 wrote: > On 01/02/2016 12:23, Stefan Ram wrote: > [...] >> final int i; >> i = 3; > > This is illegal. No. It is valid Java. > final int i = 3; > > is the correct form: initialization. Both forms are correct (assuming correct means: valid syntax and well defined semantics). In this particular case it is the best form. Bit in more complex cases with if and switch then the original form becomes necessary. > 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). Ref: https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.4 Arne