开发者

What is the use of the static modifier in object-oriented programming?

In one of my interviews, I was asked what the static modifier signifies. I replied by telling the interviewer that static class's object cannot be created and other useful points.

But the interviewer asked what is the use of creating such a class whose objects cannot be created. Basically, they were asking why is static needed in the first 开发者_StackOverflowplace?

I'm not really sure how to answer that question. What should I have said?


The interviewer probably wanted you to discuss object-oriented design and patterns, more so than they wanted you to recite the definition of that particular modifier. There's really no right answer here. Purists might argue that static is an abomination. Pragmatists might argue that it fills a gaping hole in the "everything is an object" abstraction, allowing you to call utility methods for which it doesn't make sense to instantiate a new object just to call them. The canonical example of this is the System.Math class.

The general rule of thumb that most programmers follow is that if the data you're operating on is not associated with any particular instance of an object, it probably makes sense for that field/method to be marked as static. Otherwise, it should probably be a regular member of the object instance.

The MSDN documentation has a pretty good explanation already:

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes. For more information, see Static Classes and Static Class Members (C# Programming Guide).

The static modifier also has more specific uses in C#, such as defining extension methods (which can only be defined inside of a static class), defining interop methods, etc. It's also worth noting that all static classes are sealed in C#, because without a constructor, they cannot be inherited from.


Static doesn't just apply to classes, members can be static too. The reason for using static is for providing utility type functionality that doesn't make sense to instantiate an object to use it. E.g. Why would you want to create an int in order to use int.Parse()


You can look at "static" with respect to the state of an object/class.

  1. "static" is used when you have a class which doesn't need to maintain state information for an individual object but instead state information be maintained for the class. Examples are static variables.
  2. The other thing is declaring a class itself as static where in object can not be instantiated instead, you can call the static methods which need not have its own state information.

Others have given some examples which are useful and I will not reiterate.


See http://msdn.microsoft.com/en-us/library/79b3xss3%28v=vs.80%29.aspx - it explains the advantages of static classes.

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.


The static modifier means that a member of the class belongs to the class itself rather than to instances of the class.

The static member cannot be replicated in application even if many instances of the class are created.

see the following link for more info Static Modifiers

See the following example class using static members:

class myClass
{
    static int x = 5;
    static string name = "Ashraf";

    public int X 
    {
        get {return x;}
        set {x = value;}
    }

    public string name 
    {
        get {return name;}
        set {name = value;}
    }

}


Static classes are useful when they operate on external data and don't need to set or retrieve any field(s) in them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜