Using static class to separate data and behavior independent of any object

Hi

A few days back I was having a conversation with my colleague on the use of static class. At that point I decided to write a post on the issue giving an example on when to use a static class and members.

We use static class when we have to separate data and behavior that will be independent of any object identity. The data and functions do not change regardless of what happens to the object.

A static class can only contain static members. We cannot create an instance of a static class. Static class is always sealed and they cannot contain instance constructor. Hence we can also say that creating a static class is nearly same as creating a class with only static members and a private constructor (private constructor prevents the class from being instantiated).

The advantage of using the static class is that compiler can check that no instance of the class is accidentally added. The complier will not allow any instance class of a static class. We also cannot inherit a static class since it is sealed. Static class do not have constructor, but can still declare static constructor to set up the initial values.

Static class also makes the implementation simpler and faster since we do not have make and instance of the class to call its method.  An example of good use of static class would be a class like math, which does all the mathematic function, or a currency converter class to convert currency class for converting the currency.

Hope this helps
Thanks
Vikram


Share this post   Email it

Feedback

Posted on 3/10/2009 9:23:45 AM

Your post is not very helpful sorry to say so, what advantage ever does we get from a static class is not obvious to me yet. System.Math class example is there on Microsoft web page as well, but its really not clarifying the real concept.

Please write more explanation to it so that we can all learn.

Posted on 10/5/2009 11:29:25 PM

ok,but not clarity.Give me one example

Posted on 10/20/2009 6:09:52 PM

Generic:
1.This is a parameter type.
2.Run time will deside data type,no need boxing
3.Type safe,faster

same feature got through strong arraylist and object.
why need generic?

Posted on 2/6/2010 1:21:00 PM

Thank you very much! I created a nice little LINQ SaveChanges method. Database.SaveChanges(); which I think is much 'prettier'.

Posted on 7/21/2010 10:12:50 AM

Hi, gud material abt Static class...

Regards,
Alim

Posted on 10/13/2010 1:06:56 PM

its nice...

Posted on 12/24/2010 12:25:30 PM

Hi
It is good comment to understand what is static class. But where we can use static class, whenever we can't instanciate
and can't inherit the static class?

Posted on 1/24/2011 11:40:38 AM

Sorry to say but this article is gud only for beginners as it only tells you the basics of a static class. It should've been explained with a scenario based example.

Posted on 2/18/2011 8:55:39 PM

Hi vikram . I agree its a good post but is for beginners. so please post the main feature why and when to use static class.thanks

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Copyright © 2006 - 2012 Vikram Lakhotia