We help IT Professionals succeed at work.

all static methods can be used in normal class then why static class

praveen1981
praveen1981 asked
on
Hi

here i can use all the static variables, static methods and static constructor in the normal class , then why the static

Class is Required  what is the exact difference, as i know that  static class will load in to the memory at the time of application loads, is that only difference, then when the static method in the normal class will loads in to the memory.

Please clarify.
Comment
Watch Question

Kiran SonawaneProject Lead
CERTIFIED EXPERT
Top Expert 2011

Commented:
1)

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, 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 UtilityClass that has a public method named MethodA, you call the method as shown in the following example:
UtilityClass.MethodA();

Open in new window


2) When to use static class

Basically they are used when they just contain fairly simple helper methods



They're very easy to use, no instantiation, no disposal, just fire'n'forget.
nishant joshiTechnology Development Consultant
CERTIFIED EXPERT

Commented:
static class does have constructor.

Author

Commented:
Hi

But what ever we are achieving  with static class we can achieve with static methods or static constructors or static variables from normal class, then why exactly i requrired the static class.
Sr. Developer
CERTIFIED EXPERT
Commented:
There actually is a static constructor... but it's best not to use it unless you know what you're doing, because there's no guarantee as to when the constructor will run (without using specific patterns).

Bing or Google it and you'll see what I mean. Here's what MSDN has to say:

"A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.

Static constructors have the following properties:
- 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.
- A static constructor cannot be called directly.
- The user has no control on when the static constructor is executed in the program.
- A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
- Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
- If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running."

In my opinion, the reason to use static classes is when you have static methods that require other "things" within that class. Non-static members cannot be accessed by static members, and you'll see compiler errors if you try to do this.

A well-respected colleague of mine says that "static classes are evil". I wouldn't go so far as to say that, but I do think that they should be avoided unless the architecture of the solution was created with the deliberate intent of using them in the first place.

-Happy Coding! :)
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
If you can achieve everything you were doing with the static class using a regular class then you didn't need the static class.

One non-technical difference would be convenience.  With the static class you don't need to instantiate it and you can access everything from anywhere in the application.  For the simplest of projects this is helpful as you don't have to pass a reference to the class around.  There is valid use for static members...but it sounds like you don't need them for your current situation.
Kelvin McDanielSr. Developer
CERTIFIED EXPERT

Commented:
I agree with Idle Mind. Let me also add (and this is important in some cases) that static classes are not thread-safe out of the box.

Author

Commented:
I was not completely satisfy with the answer
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
"I was not completely satisfy with the answer"

...then ask more follow-up questions next time until you are satisfied.  =)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.