Click here to Skip to main content
15,892,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all ,
if static class can access methods without instializing object ,then why
to use Non-static class .?
when static class could not be inherited ,why and where we use static class ?
can any help me on this issue ..
thanks in advance
Posted

If a class is declared as static then the variables and methods should compulsorily be declared as static.

A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.

->The main features of a static class are:

They only contain static members.
They cannot be instantiated.
They are sealed.
They cannot contain Instance Constructors or simply constructors as we know that they are associated with objects and operates on data when an object is created.
 
Share this answer
 
A static class serves as a convenient way to group (usually) related methods into a single "toolbox". In the code base I've built up over the years, I have a static class that encapsulates code for PGP, another that manages regular expression constants and can generate Regex objects using those constants, another that encapsulates Windows COM API calls and yet another that contains data conversion functions.
 
Share this answer
 
The "static" key word for a class is just a handy fool-proof syntax element. It helps you to get early error message if you try to put a single instance (non-static) member by accident. By definition, static class simply requires all its members to be static (except nested classes).

In contrast to that, static members of the class really have essential semantic meaning. The non-static members are also called instance member. An instance field is a member of the instance, so a separate field per class instance exists. In contrast, only one static field per Application Domain is created. Static methods do not have the hidden "this" parameter representing the class instance, so they cannot address any non-static members.

I explained it all in further detail in my past answers:
What makes static methods accessible?[^],
Catch 22 - Pointers to interface objects die when function using them is made static.[^],
C# windows base this key word related and its uses in the application[^].

—SA
 
Share this answer
 
Hi
Well you are right classes marked static do belong to any instance. Well it all depends on your design pattern. Like take me for example, if i am developing a application that interfaces with a database. When retrieving data from the database i create a static class that is responsible for the necessary database functions. Of those retrieved records i use them to create new objects using classes that aren't marked static.

I hope i have answered your question
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-May-13 10:07am    
This is quite apparent that you don't understand static classes (and classes in general) yourself, otherwise you would try to explain why not using a non-static class for the same exact purpose you described. (And this is actually possible, and using a static class is not required.)

So, why answering on something you don't understand yourself? Confusing of inquirers is a bad thing. Not answering would be much better help than answering.

—SA
pramodkumarw 30-May-13 14:04pm    
hi,
do u Know more About Static Class .means Why We static class when the Normal Class Is Exist
Sergey Alexandrovich Kryukov 30-May-13 14:31pm    
Sure. Please see Solution 5.
Thank you,
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900