create instance method python code example
Example 1: What is the difference between static (class) method and instance method?
static or class method
1) A method that is declared as static is known as the static method
2) We don't need to create the objects to call the static methods
3) Non-static (instance) members cannot be accessed in static context
(static method, static block and static nested class) directly.
4) For example: public static int cube(int n){
return n*n*n*; (multiply *) }
Instance method
1) A method that is not declared as static is known as the instance method.
2) The object is required to call the instance method.
3) Static and non-static variables both can be accessed in instance methods.
4) For example: public void msg() {
...}.
Example 2: call instance class python
class example:
def __call__(self):
print("It worked!")
g = example()
g()
Example 3: instance method in python
class Student:
def __init__(self, a, b):
self.a = a
self.b = b
def avg(self):
return (self.a + self.b) / 2
s1 = Student(10, 20)
print( s1.avg() )