Path: csiph.com!usenet.pasdenom.info!news.etla.org!nntp-feed.chiark.greenend.org.uk!ewrotcd!reality.xs3.de!news.jacob-sparre.dk!loke.jacob-sparre.dk!news.thorslund.org!pnx.dk!border3.nntp.ams.giganews.com!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.bt.com!news.bt.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 27 Apr 2013 03:25:05 -0500 Date: Sat, 27 Apr 2013 09:25:04 +0100 From: lipska the kat <"nospam at neversurrender dot co dot uk"> Organization: Trollbusters 3 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: A small exercise References: <5172e967$0$32109$14726298@news.sunsite.dk> <517b3461$0$32112$14726298@news.sunsite.dk> In-Reply-To: <517b3461$0$32112$14726298@news.sunsite.dk> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Message-ID: <2rWdnWTV7bz8FubMnZ2dnUVZ7qGdnZ2d@bt.com> Lines: 93 X-Usenet-Provider: http://www.giganews.com X-AuthenticatedUsername: NoAuthUser X-Trace: sv3-8MCpTz1I552SEmapDZRX12ObYcsghaEYByUWTkyPsC/NhDkaYzbN2byUyh2D0mdbf5jd2cnDbOJb36c!CbxepcpyguiJdnS4fg+9RYTGl9W72X6nZWyLjrdfzPLAxdYDSGHHIbWY3jLjtr16V5JG9bbnBIo= X-Complaints-To: abuse@btinternet.com X-DMCA-Complaints-To: abuse@btinternet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 4770 Xref: csiph.com comp.lang.java.programmer:23666 On 27/04/13 03:13, Arne Vajhøj wrote: > On 4/21/2013 6:04 AM, lipska the kat wrote: >> On 20/04/13 20:15, Arne Vajhøj wrote: >>> On 4/20/2013 12:02 PM, lipska the kat wrote: >>>> On 20/04/13 14:11, Stefan Ram 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.) >>>> >>>> open-jdk6 and open-jdk7 >>>> >>>> I think that logically, Main.this.b refers to the enclosing *instance* >>>> which implies that the class has been instantiated, final fields can >>>> only be initialized once in a constructor so trying to (logically) >>>> assign a value to a final variable after instantiation is illegal. >>>> >>>> I think :-| >>>> >>>> If you remove the final modifier from b then the code compiles, but >>>> then >>>> you know this already. >>> >>> My understanding of the question is that it is why a work but not b >>> as this and Main.this should be the same. >> >> Yes, this is what I understood. >> >> I just don't think that >>this<< and >>Main.this<< are logically >> equivalent. >> >> >>this<< refers to the current instance, >> >> >>Main.this<< refers to the (logically) enclosing instance which >> implies that the >>Main.this.b<< assignment occurs after instantiation >> which would be illegal which is why the compiler complains. > > For the given code this and Main.this refer to the same object. Ah yes, the old 'state the obvious to discredit the poster' technique ... weak (and incorrect, see below). Instead of making incorrect observations try to come up with a theory of your own. Here's an expansion of my theory Java-7-openjdk, Eclipse Juno The following code snippet gives 2 compile time errors final int a; final int b; public Main(){ this.a = 0; Main.this.b = 0; } the errors are, "the blank final field b may not have been initialized" and "the final field Main.b cannot be assigned" This *implies* to me that this.b and Main.this.b are not *logically equivalent*, this is what I said, nothing to do with whether "For the given code this and Main.this refer to the same object." that is entirely a fabrication of your own confused mind, in any case, I would never have used the work 'object' in this context, I would use the word instance, not the same thing at all. If this and Main.this not equivalent then they must be different, now all we need to figure out is what the difference is. I say that the difference is that the construct Main.b *logically* refers to a concrete instance of Main which implies that Main has been instantiated which means that you are trying to initialize a blank final variable outside the class constructor which is illegal. As usual I'm happy to be proved wrong as it will mean I have learned something which implies that I don't know everything which I have no difficulty with. lipska -- Lipska the Kat©: Troll hunter, sandbox destroyer and farscape dreamer of Aeryn Sun