Static Class, Static Class Member and Static Constructor

Static Class:

The static modifier can be used with classes, fields, methods, properties, operators, events and constructors but it cannot be used with indexers, finalizers or types other than classes.

A static class cannot be instantiated means you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

For example: if you have a static class that is named class1 that has a public method named method1, you call the method as shown in the following class1.method1();

Static class contains only static members. So creating a static class is the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated. The compiler will guarantee that instances of static class cannot be created.

Static Class Member:

Use the static modifier to declare a static member which belongs to the type itself rather than to a specific object. The static member is always accessed by the class name not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods can be overloaded but not overridden, because they belong to the class, and not to any instance of the class.

Static constructors: 

A static constructor does not take access modifiers or have parameters. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. Static constructor runs only one time. It runs before any instance constructor runs.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s