๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #715

Re: A question about inner classes

#715
From: Carlos Zin
Date: Fri, 03 Dec 2004 12:25
69 lines
3408 bytes
Thanks B-team for all your support, this newsgroup is a invaluable source of 
learning.
I entered into a panic attack when I changed the classes to static and still 
got the error   " non static variable this etc. etc.." but then I rebuilded 
the project and the error disappeared. I think this is a little JBuilder's 
bug.
Thanks again to all of you
CZ


"John McGrath [TeamB]" <ng-b@JPMcGrath.net> escribi๏ฟฝ en el mensaje 
news:xn0dqk80xq9wi001@newsgroups.borland.com...
> On 12/2/2004 at 6:41:23 PM, Carlos Zin wrote:
>
>> public class ToolFactory {
>>
>>     private class A extends Tool {}
>>
>>     private class B extends Tool {}
>>
>>     public static Tool makeTool(String toolName) {
>>         if (toolName == "A")
>>             return new A();
>>         else if (toolName == "B")
>>             return new B();
>>         else
>>             return null;
>>     }
>> }
>>
>> I am trying to build a static factory method named "makeTool()" which
>> produces instances of classes A and B but the compiler complain telling
>> that non-static variable this cannot be referenced from a static context.
>
> Classes A and B are non-static.  That means that require an enclosing
> instance of class ToolFactory, and that is passed to their constructors as
> a hidden argument.  From a non-static context of ToolFactory, the compiler
> will automatically pass the current instance (this) to the constructor.
> From a static context or from outside of class ToolFactory, you must
> specify the ToolFactory instance to provide to the constructor.  That is
> done using the syntax:
>
>    toolFactoryInstance.new A();
>    toolFactoryInstance.new B();
>
> If these classes need to reference non-static members of ToolFactory, you
> will need to provide the enclosing instance to the constructor as shown
> above.  If not, just declare classes A and B as static.
>
> One more note:
>
>>         if (toolName == "A")
>
>>         else if (toolName == "B")
>
> I think you want to use:
>
>          if (toolName.equals("A"))
>
>          else if (toolName.equals("B"))
>
> -- 
> Regards,
>
> John McGrath [TeamB]
>
> ---------------------------------------------------
> Before sending me e-mail, please read:
>    http://www.JPMcGrath.net/newsgroups/e-mail.html 
Technical Details
Message-ID: <41b05af9$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news.zanker.org!feeder1.cambrium.nl!feed.tweaknews.nl!amsnews01.chello.com!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <41afa7d8$1@newsgroups.borland.com> <xn0dqk80xq9wi001@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,8b4c0601b22a435f
X-Google-Attributes: gidf74b3,public
X-Google-Language: ENGLISH,ASCII
From: "Carlos Zin" <nomailplease@hotmail.com>
Newsgroups: borland.public.jbuilder.java.language
References: <41afa7d8$1@newsgroups.borland.com> <xn0dqk80xq9wi001@newsgroups.borland.com>
Subject: Re: A question about inner classes
Date: Fri, 3 Dec 2004 08:25:35 -0400
Lines: 71
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
NNTP-Posting-Host: 200.109.212.207
Message-ID: <41b05af9$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1102076665 200.109.212.207 (3 Dec 2004 04:24:25 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news.zanker.org!feeder1.cambrium.nl!feed.tweaknews.nl!amsnews01.chello.com!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail