Click here to Skip to main content
15,909,051 members
Please Sign up or sign in to vote.
4.40/5 (3 votes)
See more:
why an instance of a static class cannot be created?where memory for static class is allocated? what process take place when instance constructor is called?
Posted

Static Classes and Static Class Members (C# Programming Guide)[^]
That should help understand what static classes are
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Sep-11 17:28pm    
Good reference which should help to learn and understand things, my 5.

As to my solution, I tried to answer each question to the point. I realize that this is not the best way to learn things; and this set of question does not cover the topic systematically. Also, 3rd question is a re-post; so I referenced my recent solution. Please see both :-)
--SA
Simon Bang Terkildsen 20-Sep-11 18:14pm    
I got the feeling the OP didn't know what static classes were because he talked about instance constructors in that context, I see now that it were probably seperate questions.
Your answer is very good and detailed, I think the OP should now have a much better understanding of the subjects.
Sergey Alexandrovich Kryukov 20-Sep-11 18:50pm    
Thank you, Simon.
I hope so, too. There is nothing wrong with these questions, but another one shows some serious confusion in OP. It needs some more systematic study first, and then the questions will be more productive. It's good that Hena is trying to get to the essence of things, not trying to do the tricks by trial-and-error as many do.
--SA
Many clever answers can be given, but the essence of things about first answer is very simple: the only goal of adding of a keyword static to a class is exactly this: to disable creation of its instance. There is no any special mechanism, this is just syntax.

You can talk about the purpose of it. This is just sanity. If a class has non-static members, creating of its instance is possible but pretty much useless. Claiming the class itself as static is just an additional safeguard to prevent creation of class. Also, if a class is already declared static, adding non-static members accidentally will be prevented by a compiler.

Allocation… You see, you cannot say anything about certain about allocation of the arbitrary static class. Suppose you create a static class with only the members of value types. In this case, nothing is "allocated", the memory of the static data segment will be used — well, statically. But now, the static class can have only the static members, but any of the members can be of any class, and that classes may be not static; and their members can be static or not static, of value types or reference types, so some of the memory can be allocated on heap.

Last question. When an instance constructor is called, a new instance of the class is created. The reference of the instance is assigned to the variable on right, if any. It may not exist as the constructor call could be done just for its side effect; in this case the instance may or may not be a subject of Garbage Collection. There can be (pretty unusual) case when this reference is not lost, as "this" reference is created before the body of the constructor is executed, and it can be assigned to some other type member or variable outside of the class scope. I already provided you with other detail in my recent solution, see memory allocation when instance class constructor is called[^].

—SA
 
Share this answer
 
v4

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