Python
class User():
def __init__(self, username):
self.username = username
@staticmethod
def what_am_i():
return 'I can be called by User class and also by any User instance'
User.what_am_i()
Javascript
class User {
constructor(username) {
this.username = username;
}
static whatAmI() {
return 'I can be called only by User class'
}
}
User.whatAmI();
Thing to note
In python if we declare a static method it can be called by both the class and by any of the instances (objects) created from the class ✔️✔️✔️
Whereas in Javascript though, if we declare any static methods, it can only be accessed by calling it from the class and not by its instances 📣✔️✔️✔️
Top comments (0)