Skip to main content
added 424 characters in body
Source Link
gbjbaanb
  • 48.8k
  • 7
  • 106
  • 174

Lets explain it with a hypothetical sample.

Imagine a simple class:

class User
{
User(string n) { name = n; };
string name;
}

Now we create 2 instances of this class:

User Bones = new User("Bones");
User Jim = new User("Jim");

now, think - what if we add a new static method to User, eg:

static string GetName();

and you call it:

string x = User::GetName()

what would x contain? "Jim", "Bones", or something else?

The problem is that a static method is a single method, defined on the class, not the objects. As a result, you don't know which object it might apply to. This is why its a special thing. Its best to think of static methods as individual things, like functions in C for example. That languages like Java have them contained inside classes is mainly a problem with Java not allowing anything to exist outside a class, so functions like this have to be forced inside a class in some manner (a bit like how main() is forced to be inside a class too when all sense says it should be a singular, standalone function).

Lets explain it with a hypothetical sample.

Imagine a simple class:

class User
{
User(string n) { name = n; };
string name;
}

Now we create 2 instances of this class:

User Bones = new User("Bones");
User Jim = new User("Jim");

now, think - what if we add a new static method to User, eg:

static string GetName();

and you call it:

string x = User::GetName()

what would x contain? "Jim", "Bones", or something else?

The problem is that a static method is a single method, defined on the class, not the objects. As a result, you don't know which object it might apply to. This is why its a special thing.

Lets explain it with a hypothetical sample.

Imagine a simple class:

class User
{
User(string n) { name = n; };
string name;
}

Now we create 2 instances of this class:

User Bones = new User("Bones");
User Jim = new User("Jim");

now, think - what if we add a new static method to User, eg:

static string GetName();

and you call it:

string x = User::GetName()

what would x contain? "Jim", "Bones", or something else?

The problem is that a static method is a single method, defined on the class, not the objects. As a result, you don't know which object it might apply to. This is why its a special thing. Its best to think of static methods as individual things, like functions in C for example. That languages like Java have them contained inside classes is mainly a problem with Java not allowing anything to exist outside a class, so functions like this have to be forced inside a class in some manner (a bit like how main() is forced to be inside a class too when all sense says it should be a singular, standalone function).

Source Link
gbjbaanb
  • 48.8k
  • 7
  • 106
  • 174

Lets explain it with a hypothetical sample.

Imagine a simple class:

class User
{
User(string n) { name = n; };
string name;
}

Now we create 2 instances of this class:

User Bones = new User("Bones");
User Jim = new User("Jim");

now, think - what if we add a new static method to User, eg:

static string GetName();

and you call it:

string x = User::GetName()

what would x contain? "Jim", "Bones", or something else?

The problem is that a static method is a single method, defined on the class, not the objects. As a result, you don't know which object it might apply to. This is why its a special thing.