Why is Static method is called using the class name and not by an object?
Since I recall this question, let me post this to keep myself remember this stuff.
Well, ideally, a static class is called using it's class name.
So when saying,
class A {
...
public static int mystaticmethod() {
...
}
}
then the encourage call is A::mystaticmethod(), right?
Well the reason why is that, static binding [or early binding] is done at compiled time, and not by run-time. So it's very ideal to call the A class than an object, let say "A a = new A();" then call it as a.mystaticmethod() which is strongly discouraged.
So anyway, that's the reason why calling a static method is thru it's class name and not by an object. Object is done at run-time, where dynamic binding is done in that process.
Well, ideally, a static class is called using it's class name.
So when saying,
class A {
...
public static int mystaticmethod() {
...
}
}
then the encourage call is A::mystaticmethod(), right?
Well the reason why is that, static binding [or early binding] is done at compiled time, and not by run-time. So it's very ideal to call the A class than an object, let say "A a = new A();" then call it as a.mystaticmethod() which is strongly discouraged.
So anyway, that's the reason why calling a static method is thru it's class name and not by an object. Object is done at run-time, where dynamic binding is done in that process.
Comments