static classes, nested class, public class

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
P

puzzlecracker

Would you quickly remind me the difference between, regular class,
static class, and nested class?

Thanks
 
puzzlecracker said:
Would you quickly remind me the difference between, regular class,
static class, and nested class?

A 'regular' class my be used to create object instances for which the class
is the type. Each instance will hold a set of fields which hold values for
that instance only. A class may also contain static members which may be
fields, properties or methods which are common across all instances of the
class. Such static members are accessed through the Type name.

A class marked as static may only contain static members and instances of a
static class cannot be created.

A nested class is a class that is defined inside another class. One
difference that a nested class has from a regular class is that it may be
private where it can only be used inside the class definining it, whereas a
regular class cannot be private.
 
A 'regular' class can contain any kind of members (fields, methods,
properties, events et cetera), be it static or accessible through an
instance only. A static class, however, can only contain static members and
this is especially useful when the design you choose assumes that this class
cannot or should not be instantiated, for example a set of helper functions.

A nested class is defined within the scope of its 'parent' class--it may be
private or accessible to external classes. You might want to choose to do
this if your design assumes a hierarchical relationship between the two (or
more) classes.
 
Anthony said:
A 'regular' class my be used to create object instances for which the
class is the type. Each instance will hold a set of fields which
hold values for that instance only. A class may also contain static
members which may be fields, properties or methods which are common
across all instances of the class. Such static members are accessed
through the Type name.
A class marked as static may only contain static members and
instances of a static class cannot be created.

A nested class is a class that is defined inside another class. One
difference that a nested class has from a regular class is that it
may be private where it can only be used inside the class definining
it, whereas a regular class cannot be private.

Also, being a member of the containing class, it has permission to use
private and protected members of the containing class. Static members are
of course accessible directly, and instance members can be explicitly
referenced using a reference to an object derived from the containing class.
Note that these are NOT Java inner classes, there is no implicit reference
to an instance of the containing class.

And Java uses the static keyword very differently with member classes --
Java defaults to defining an "inner class" and you use "static" to get a
normal nested class. C# always gives you a nested class.
 
Back
Top