197

What does it mean when you add the static keyword to a method?

public static void doSomething(){
   //Well, do something!
}

Can you add the static keyword to class? What would it mean then?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Moshe
  • 57,511
  • 78
  • 272
  • 425
  • 12
    You can use static method without creating an instance of that class simply by class_name.static_method_name(); – Javed Akram Nov 08 '10 at 13:27
  • At programming level, we get a feeling that we're able to call a static method without creating an instance of a class/type. Internally it is _not_ the case. CLR internally manages a special instance called _type instance_ for managing call to static methods. Please see [this](https://stackoverflow.com/a/40276624/465053) answer. It is so intriguing. – RBT Feb 07 '19 at 09:38

9 Answers9

343

A static function, unlike a regular (instance) function, is not associated with an instance of the class.

A static class is a class which can only contain static members, and therefore cannot be instantiated.

For example:

class SomeClass {
    public int InstanceMethod() { return 1; }
    public static int StaticMethod() { return 42; }
}

In order to call InstanceMethod, you need an instance of the class:

SomeClass instance = new SomeClass();
instance.InstanceMethod();   //Fine
instance.StaticMethod();     //Won't compile

SomeClass.InstanceMethod();  //Won't compile
SomeClass.StaticMethod();    //Fine
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 35
    So it's like a class method instead of an instance method? – Moshe Nov 08 '10 at 13:12
  • 6
    @Moshe: Exactly. With a static method you do not need an instance of the class to call the method, just the class. – Binary Worrier Nov 08 '10 at 13:14
  • 3
    But is there actually some kind of technical limitation that prevents calling a static method on an instance? If the compiler would allow it, what is the danger of it being accessible? – kroonwijk Sep 12 '11 at 19:09
  • 3
    @kroon: It wouldn't make any sense. Instance methods actually just take an instance as a hidden first parameter. Static methods don't. See my blog post: http://blog.slaks.net/2011/06/open-delegates-vs-closed-delegates.html – SLaks Sep 12 '11 at 20:28
  • Where "StaticMethod" is Class method and "InstanceMethod" is an Instance Method of Class SomeClass – Usman Younas Jan 15 '15 at 22:39
30

From another point of view: Consider that you want to make some changes on a single String. for example you want to make the letters Uppercase and so on. you make another class named "Tools" for these actions. there is no meaning of making instance of "Tools" class because there is not any kind of entity available inside that class (compare to "Person" or "Teacher" class). So we use static keyword in order to use "Tools" class without making any instance of that, and when you press dot after class name ("Tools") you can have access to the methods you want.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Tools.ToUpperCase("Behnoud Sherafati"));
        Console.ReadKey();
    }
}

public static class Tools
{
    public static string ToUpperCase(string str)
    {
        return str.ToUpper();

    }
}
}
  • 1
    Using "making changes on a string" is a bad example - strings are immutable and cannot be changed. But, otherwise, the explanation makes sense (substituting a non-immutable class for string) – Flydog57 Feb 08 '19 at 22:41
  • @Flydog57 non-immutable is just mutable hahahaha – Mindless Sep 17 '22 at 05:49
13

A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine. Static class members are declared using the static keyword before the return type of the membe

Alborz
  • 2,043
  • 2
  • 20
  • 17
5

Shortly you can not instantiate the static class: Ex:

static class myStaticClass
{
    public static void someFunction()
    { /* */ }
}

You can not make like this:

myStaticClass msc = new myStaticClass();  // it will cause an error

You can make only:

myStaticClass.someFunction();
r.mirzojonov
  • 1,209
  • 10
  • 18
5

Static function means that it is associated with class (not a particular instance of class but the class itself) and it can be invoked even when no class instances exist.

Static class means that class contains only static members.

mih
  • 515
  • 3
  • 13
0

When you add a "static" keyword to a method, it means that underlying implementation gives the same result for any instance of the class. Needless to say, result varies with change in the value of parameters

Balaji
  • 21
  • 6
0

Core of the static keyword that you will have only one copy at RAM of this (method /variable /class ) that's shared for all calling

Mahmoud
  • 60
  • 4
-1

Static variable doesn't link with object of the class. It can be accessed using classname. All object of the class will share static variable.

By making function as static, It will restrict the access of that function within that file.

kapilddit
  • 1,729
  • 4
  • 26
  • 51
-8

The static keyword, when applied to a class, tells the compiler to create a single instance of that class. It is not then possible to 'new' one or more instance of the class. All methods in a static class must themselves be declared static.

It is possible, And often desirable, to have static methods of a non-static class. For example a factory method when creates an instance of another class is often declared static as this means that a particular instance of the class containing the factor method is not required.

For a good explanation of how, when and where see MSDN

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Dave Arkley
  • 45
  • 1
  • 4
  • 8
    No, a static class is *never* instantiated. Given that everything in it is static, why would you *want* to instantiate it? – Jon Skeet Nov 08 '10 at 13:17
  • 1
    A `static` class has no instance at all. – SLaks Nov 08 '10 at 13:17
  • 4
    Sorry guys, I don't understand...I said a single instance is created and you can't new one up. Surely a single, static, instance is created otherwise the code wouldn't be callable? – Dave Arkley Nov 08 '10 at 13:57
  • 2
    A static class does have an instance, in fact two, they just aren't instances of theType. A static class will exist on the heap as a [Foo] Type object (method lookup table etc for the JIT), and a special System.Type object used for initialization. – mccainz May 21 '13 at 19:50
  • 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. – Satheesh Sep 03 '14 at 18:16