static class

Jonathan M Davis jmdavisProg at gmx.com
Sun Feb 17 14:25:04 PST 2013


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. That's not what it means for a 
class to be static at all. The only place that static has any effect on classes 
is for nested classes. A static, nested class is like any other class except 
that it's inside another class, which affects its path. e.g.

module b;

class C
{
    static class W
   {
   }
}

W is therefore b.C.W, whereas if it were not nested inside of C but next to it 
within the same module, then it would be b.W.

However, non-static nested classes are associated with an instance of the 
outer class, and therefore only one can exist per class instance. However, it 
also has access to that outer class instance through the property outer. e.g.

class C
{
    class R
    {
        void func()
        {
            //Sets the variable in the instance of
            //C that it's associated with.
            outer.i = 7;
        }
    }

    int i;
}

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list