static classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • puzzlecracker

    static classes

    It looks like static classes can have non-static member classes --
    does it make sense what I just wrote? If not, let me illustrate it:

    public static class Foo{

    private struct Bar{

    }

    }

    I wonder why this is allowed.

    Also, what is the difference between static and non-static nested
    classes?

    public class Foo{

    private class Bar
    {

    }
    }

    vs.
    public class Foo{

    static private class Bar
    {

    }
    }

    thanks
  • Jon Skeet [C# MVP]

    #2
    Re: static classes

    On Oct 16, 3:41 pm, puzzlecracker <ironsel2...@gm ail.comwrote:
    It looks like static classes can have non-static member classes
    Yes.
    does it make sense what I just wrote? If not, let me illustrate it:
    >
    public static class Foo{
    >
       private struct  Bar{
    >
      }
    >
    }
    >
    I wonder why this is allowed.
    Why wouldn't it be? In some ways it's still "static" in that a nested
    type isn't associated with any instance of its containing type. Note
    that this is different from inner classes in Java.
    Also, what is the difference between static and non-static nested
    classes?
    >
    public class Foo{
    >
       private class Bar
       {
    >
      }
    >
    }
    >
    vs.
    public class Foo{
    >
      static  private class Bar
       {
    >
      }
    >
    }
    The latter is just like any other static class: it can only have
    static members, it doesn't have any constructors (not even an
    autogenerated one), you can't declare a variable of that type, etc.

    Jon

    Comment

    • puzzlecracker

      #3
      Re: static classes

      On Oct 16, 11:06 am, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
      On Oct 16, 3:41 pm, puzzlecracker <ironsel2...@gm ail.comwrote:
      >
      It looks like static classes can have non-static member classes
      >
      Yes.
      >
      does it make sense what I just wrote? If not, let me illustrate it:
      >
      public static class Foo{
      >
         private struct  Bar{
      >
        }
      >
      }
      >
      I wonder why this is allowed.
      >
      Why wouldn't it be? In some ways it's still "static" in that a nested
      type isn't associated with any instance of its containing type. Note
      that this is different from inner classes in Java.
      >
      >
      >
      Also, what is the difference between static and non-static nested
      classes?
      >
      public class Foo{
      >
         private class Bar
         {
      >
        }
      >
      }
      >
      vs.
      public class Foo{
      >
        static  private class Bar
         {
      >
        }
      >
      }
      >
      The latter is just like any other static class: it can only have
      static members, it doesn't have any constructors (not even an
      autogenerated one), you can't declare a variable of that type, etc.
      >
      Jon
      Hmm, perhaps I missing the point of static nested classes vs. non-
      static nested classes

      Comment

      • Ignacio Machin ( .NET/ C# MVP )

        #4
        Re: static classes

        On Oct 16, 10:41 am, puzzlecracker <ironsel2...@gm ail.comwrote:
        It looks like static classes can have non-static member classes --
        Of course. Why would you be forced to declare it static?
        think of this:
        static class Factory{
        public class Internal{}
        public static Factory.Interna l GetIt(){ return new Internal();}
        }
        does it make sense what I just wrote? If not, let me illustrate it:
        >
        public static class Foo{
        >
        private struct Bar{
        IIRC a struct cannot be marked as static so there goes one more issue
        of why not restrict it to statics

        Comment

        • puzzlecracker

          #5
          Re: static classes

          On Oct 16, 11:40 am, "Ignacio Machin ( .NET/ C# MVP )"
          <ignacio.mac... @gmail.comwrote :
          On Oct 16, 10:41 am, puzzlecracker <ironsel2...@gm ail.comwrote:
          >
          It looks like static classes can have non-static member classes --
          >
          Of course. Why would you be forced to declare it static?
          think of this:
          static class Factory{
            public class Internal{}
            public static Factory.Interna l  GetIt(){ return new Internal();}
          >
          }
          does it make sense what I just wrote? If not, let me illustrate it:
          >
          public static class Foo{
          >
             private struct  Bar{
          >
          IIRC a struct cannot be marked as static so there goes one more issue
          of why not restrict it to statics
          What is difference between inner static class and member nested class?

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: static classes

            On Oct 16, 5:06 pm, puzzlecracker <ironsel2...@gm ail.comwrote:
            IIRC a struct cannot be marked as static so there goes one more issue
            of why not restrict it to statics
            >
            What is difference between inner static class and member nested class?
            A static class (whether nested or not) has additional restrictions on
            it - basically, it's there as a utility class, and will never be
            instantiated. The "static" part of the declaration of a nested static
            class is very different from the static part of the declaration of a
            static variable, property etc.

            Jon

            Comment

            • puzzlecracker

              #7
              Re: static classes

              On Oct 16, 12:38 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
              On Oct 16, 5:06 pm, puzzlecracker <ironsel2...@gm ail.comwrote:
              >
              IIRC a struct cannot be marked as static so there goes one more issue
              of why not restrict it to statics
              >
              What is difference between inner static class and member nested class?
              >
              A static class (whether nested or not) has additional restrictions on
              it - basically, it's there as a utility class, and will never be
              instantiated. The "static" part of the declaration of a nested static
              class is very different from the static part of the declaration of a
              static variable, property etc.
              >
              Jon
              Well, in java nested static and nested member class are different in
              regard what they can access.... i guess csharp doesn't have
              static static class...

              class A
              {

              static static class B
              {

              }

              }

              Comment

              • Peter Duniho

                #8
                Re: static classes

                On Thu, 16 Oct 2008 10:43:40 -0700, puzzlecracker <ironsel2000@gm ail.com>
                wrote:
                Well, in java nested static and nested member class are different in
                regard what they can access....
                No, that's not true. The only difference between static nested and inner
                classes in Java is that an inner class has an implicit reference to an
                instance of the outer class. Both kinds of classes have the same access
                to members of the outer class, though of course without an implicit
                reference, the static nested class would need to be provided an explicit
                reference to work with.

                For example (in Java):

                public class Outer
                {
                private int i;

                private class Foo
                {
                public int getOuterI()
                {
                return Outer.this.i;
                }
                }

                static private class Bar
                {
                public int getOuterI(Outer outer)
                {
                return outer.i;
                }
                }
                }

                C# uses the word "static" only to mean that a class cannot be
                instantiated. It has no concept of an inner class (all nested classes are
                equivalent to Java's static nested class), so the "static" keyword isn't
                used in the same way.
                i guess csharp doesn't have
                static static class...
                Why would it? What would that mean?

                Pete

                Comment

                • puzzlecracker

                  #9
                  Re: static classes

                  On Oct 16, 2:02 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
                  wrote:
                  On Thu, 16 Oct 2008 10:43:40 -0700, puzzlecracker <ironsel2...@gm ail.com> 
                  wrote:
                  >
                  Well, in java nested static and nested member class are different in
                  regard what they can access....
                  >
                  No, that's not true.  The only difference between static nested and inner  
                  classes in Java is that an inner class has an implicit reference to an  
                  instance of the outer class.  Both kinds of classes have the same access  
                  to members of the outer class, though of course without an implicit  
                  reference, the static nested class would need to be provided an explicit  
                  reference to work with.
                  >
                  For example (in Java):
                  >
                  public class Outer
                  {
                       private int i;
                  >
                       private class Foo
                       {
                           public int getOuterI()
                           {
                               return Outer.this.i;
                           }
                       }
                  >
                       static private class Bar
                       {
                           public int getOuterI(Outer outer)
                           {
                               return outer.i;
                           }
                       }
                  >
                  }
                  >
                  C# uses the word "static" only to mean that a class cannot be  
                  instantiated.  It has no concept of an inner class (all nested classes are  
                  equivalent to Java's static nested class), so the "static" keyword isn't  
                  used in the same way.
                  >
                  i guess csharp doesn't have
                  static static class...
                  >
                  Why would it?  What would that mean?
                  >
                  Pete
                  Pete, thanks that explains....

                  Comment

                  • Petar Repac

                    #10
                    Re: static classes

                    puzzlecracker wrote:
                    It looks like static classes can have non-static member classes --
                    does it make sense what I just wrote? If not, let me illustrate it:
                    >
                    public static class Foo{
                    >
                    private struct Bar{
                    >
                    }
                    >
                    }
                    >
                    I wonder why this is allowed.
                    >
                    Because Foo is static you can't create instances of this type, but Foo
                    can make instances of Bar, for example is it's static fields.

                    Also, what is the difference between static and non-static nested
                    classes?
                    >
                    public class Foo{
                    >
                    private class Bar
                    {
                    >
                    }
                    }
                    >
                    vs.
                    public class Foo{
                    >
                    static private class Bar
                    {
                    >
                    }
                    }
                    >
                    thanks
                    The later just means that you can not make instances of Bar, so only Foo
                    can use it (it's private) and only call it's static members.

                    Petar

                    Comment

                    Working...