Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Context of my question:

  1. I am reading C#.
  2. Trying to understand Static keyword.
  3. I understand that when Static is applied to a member of a class, it can only be modified by the class and not the class object references.

I will take an example here.

public class BluePrint
{
   public static string BluePrintCreatorName;
}

If I need to know the BluePrintCreator's Name, I can call

BluePrint.BluePrintCreatorName;

But if a house that is created from the blueprint wants to know the BluePrintCreator's Name, it does not have access.

BluePrint NewHouse = new BluePrint();
NewHouse.BluePrintCreatorName; //This is not accessible

Compiler error says:

Member 'AccessModifier.BluePrint.BluePrintCreatorName' cannot be accessed with an instance reference; qualify it with a type name instead   

I understand this is the way it works. But I want to know the basic concept to why Static keyword was required at all?

What will go wrong if a class object reference would have access to the static member?

share|improve this question
    
Your example is not very good, since the area of a house is obviously relevant to the instance, nor shared by all instances (each house will have a different area). A better, though simple example would be just a counter variable for instances. Each time you create an instance its constructor would increase this static counter, the destructor decrease it. –  thorsten müller Jul 4 at 9:13
    
I see this is not the case in java. example here : ideone.com/4FslaY . If someone could point me to what was the reason behind not allowing this in C#. that would be my answer. –  Suyash Jul 4 at 12:38

3 Answers 3

up vote 6 down vote accepted

Nothing go wrong for accessing a static member from an instance, in fact this is perfectly posible in other languages like java, you example code compile and runs ok if its in java.

Its a check the compiler designers introduce because they think this helps programmers to write clear code with this language. This way only looking at the code you can always know if you are accessing a static or an instance member, in languages where this access is allow (like java) sometimes its a little confusing when someone decides to access a static member throughout an instance variable, in fact normally when you see this access in java code its more a mistake than a programmer really using this "language feature".

In my opinion limiting this access its a good decision in C# to avoid some mistakes and bad interpretations.

share|improve this answer
3  
FWIW although in Java it's legal to refer static members that way, it's recommended against in Code Conventions (10.2 Referring to Class Variables and Methods): "Avoid using an object to access a class (static) variable or method. Use a class name instead..." (consider editing this into your answer) –  gnat Jul 4 at 13:41

Consider the following (legal) Java code:

Thread workerThread = new MyWorkerThread();
workerThread.start();
workerThread.sleep(5000);

At first glance, it looks like the last line is requesting that workerThread sleep for five seconds. However, the Thread.sleep method is actually static, and makes the current thread sleep.

In C#, requiring static functions to be called through the class instead of an instance helps prevent writing misleading code like this.

share|improve this answer

What will go wrong if a class object reference would have access to the static member?

It is not the reference that matters, a reference is for you to know what instances your dealing with. static members work like this; they are part of a class, but they are not part of an instance derived from that class.

I understand this is the way it works. But I want to know the basic concept to why Static keyword was required at all?

Before OOP was mature, there were no runtime environments that allowed the initialization of individual objects. Programs were basically a collection of subroutines which would call each other cohesively. You can think of this as "static" to get the right idea. Even when OOP became mature, the idea of independent classes still appealed because they are accessible unconditionally. On a runtime level static classes are like instances too, but thats not how they appeal from the language. Now there is also such thing as static members, but having those in conjunction with a non-static class is unusual because it could cause unexpected behavior with asynchronous operations, note that this doesn't apply for static readonly members.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.