🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: A question about inner classes

#709
From: John McGrath [TeamB]
Date: Fri, 03 Dec 2004 06:29
57 lines
2648 bytes
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: <xn0dqk80xq9wi001@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <41afa7d8$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,8b4c0601b22a435f
X-Google-Attributes: gidf74b3,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "John McGrath  [TeamB]" <ng-b@JPMcGrath.net>
Subject: Re: A question about inner classes
Newsgroups: borland.public.jbuilder.java.language
References: <41afa7d8$1@newsgroups.borland.com>
User-Agent: XanaNews/1.16.5.2
Message-ID: <xn0dqk80xq9wi001@newsgroups.borland.com>
X-Ref: newsgroups.borland.com ~XNS:00000093
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
NNTP-Posting-Host: 141.158.45.25
Date: 2 Dec 2004 23:29:01 -0700
X-Trace: newsgroups.borland.com 1102058941 141.158.45.25 (2 Dec 2004 23:29:01 -0700)
Lines: 57
X-Authenticated-User: jpmcgrath
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail