Why we can’t create object for static class?

When i was attending my first interview in my current company this question was asked by my technical manager to me.I answered like others because the class has static keyword so we can’t create object for the class.

Again he asked but why we can’t create object to the class declared as static.Then later i have done some analysis on this and i came with following answer.

Answer:- Before we know the answer of this we need to understand that how a CLR allocate memory to objects.

Let’s say we have a concrete class called Example so when we create object for this class we’ll use of new operator like following syntax

Example example = new Example();

As soon as CLR will come across new operator it will allocate new memory location for that class called Example at Runtime and you can access member of this class using its object dynamically.

Now when we delcare a class as static The meaning of static is fixed at compile time.So the memory for that class is allocated at compile itself then why we need to create again object to class and allocate memory at runtime.

That is the reason we can’t create object for static class.

Thank You!

Advertisements

5 comments

  1. suganya · · Reply

    Nice article.

  2. suganya · · Reply

    Explain about delegates and reflection with real time Examples ?

    1. Sure suganya shortly i will post.

  3. nice

  4. Simplest way to understand….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: