static class
Steven Schveighoffer
schveiguy at yahoo.com
Sun Feb 17 17:19:45 PST 2013
On Sun, 17 Feb 2013 18:26:37 -0500, Ben Davis <entheh at cantab.net> wrote:
> On 17/02/2013 22:25, Jonathan M Davis wrote:
>> On Sunday, February 17, 2013 23:00:19 Michael wrote:
>>>> That's not the meaning of static in that context.
>>>
>>> As I understand a static class can't be instantiated.
>>
>> I have no idea how you came to that conclusion.
>
> In fairness, it is the natural guess you'd make if you haven't actively
> used nested instance classes and understood how the outer instance
> pointer is stored. We use Java at work (same mechanism as D), and hardly
> anyone actually knows to write 'static' when they create a nested class
> that they intend to be POD. :)
static class at module level means nothing. It's a noop.
static class inside a class means, this class instance does not contain a
pointer to it's outer class instance, nor does it have access to the outer
class instance's variables (naturally).
so:
module X;
static class A {}
is exactly equivalent to
class A {}
> You can make as many nested instances as you like.
Yes, as Jonathan indicated, this is possible.
> Is it possible to write someInstanceOfR.outer? I've occasionally wanted
> Java to have that feature, and ended up storing the 'outer' reference
> manually. Though this probably means I was writing bad code; I can't
> remember. :D
I don't know if outer is public or private. Quick test...
Yep, you can access it. But I don't know if that is the correct way to do
it, or if that is intended. The spec does not consider that possibility.
I will note, one VERY annoying thing about outer, is that outer by itself
doesn't work inside an inner class' function unless you qualify it with
this:
class A
{
int x;
class B
{
void foo()
{
// outer.x = 5; // Error: undefined identifier outer
this.outer.x = 5; // ok
}
}
}
-Steve
More information about the Digitalmars-d-learn
mailing list