---
title: "Static class"  
description: "In this blog, I’m explaining the concept of static class.  Static class is basically like a non-static class but it can never be instantiated. In othe"  
author: "Anchal Kesharwani"  
published: 2014-08-12  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/674/static-class  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Static class

In this blog, I’m explaining the [concept](https://www.mindstick.com/blog/79/routing-concept-in-dot-net) of static class.\

Static class is basically like a non-static class but it can never be instantiated. In other word you cannot use the new [keyword](https://www.mindstick.com/forum/33572/sql-inner-join-keyword) to create an object of a class. Because there is no [instance variable](https://www.mindstick.com/forum/23344/multidimensional-array-of-nsinteger-as-instance-variable), you access the members of a static class by using the [class name](https://www.mindstick.com/forum/12871/how-to-get-class-name) itself. For example, in the built in System.Math staic class and their [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) direct access by the name.

##### There are following features of static class:

- Contains only static members: static class same as but contains only static members and [private constructor](https://www.mindstick.com/interview/1949/where-and-how-can-you-use-a-private-constructor-in-java).
- Cannot be instantiated: a private constructor to prevent to create an instance.
- Is sealed: static classes are sealed because it cannot be inherited.
- Cannot contain instance Constructor: Static classes cannot contain an instance constructor; however, they can contain a static constructor. Non-static classes should also define a static constructor if the class contains static members that require non-trivial initialization.

##### Example 1

```
public static class staticClass    {    }       class Program    {        static void Main(string[] args)        {            staticClass obj = new staticClass(); // cannot create static class instance        }    }
```

In this example, give the [error cannot](https://www.mindstick.com/forum/12809/error-cannot-convert-from-object-to-string-when-filling-a-asp-dropdownlist-dynamically) declare a variable of static type and cannot create a instance of static class.

![Static class](https://www.mindstick.com/blogs/5db58f80-8d0a-494c-ad86-e4e7720047d9/images/91058095-204a-4924-8612-b1b85c9015dc.png)

This is the screen shot of the static class instantiation error.

##### Example 2

```
static class Rectangle    {      //All static member variables        static int width = 4;   //width of rectangle        static int height = 5;   //height of rectangle        //Member functions       public static int rectangleArea()        {           return width*height;        }    } //class end       class Test    {        static void Main(string[] args)        {                        //<ClassName>.<MethodName>            Console.WriteLine("Rectangle area is: "+Rectangle.rectangleArea());             Console.ReadKey();                   }    }
```

Output is: Rectangle area is: 20

In this example, calculate the area of rectangle by static class.

---

Original Source: https://www.mindstick.com/blog/674/static-class

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
