I want to know the difference between a call to a class method and a call to an instance method?
Isn't a class method something like:
identifier();
and a instance method like:
object.identifier();
Correct me if I'm wrong.
I want to know the difference between a call to a class method and a call to an instance method?
Isn't a class method something like:
identifier();
and a instance method like:
object.identifier();
Correct me if I'm wrong.
(I think, you mean .();
to be something like myClassInstance.myMethod();
If true, then: )
Class methods are static
methods which can be called as the class loaded. They don't need an instance of the class to be called. Assume that you created a class like this:
class MyClass {
public static void classMethod() {
/* Something to do (statically) here */
}
public void instanceMethod() {
/* Some thing to do here */
}
As you can see, once you import your class in your working area, you could call the static (class) method, like this:
MyClass.classMethod();
But not the instance method, calling the instance method needs an instance of the class to be called, like this:
MyClass mc = new MyClass();
mc.instanceMethod();
Also notice that you could only static
works (!) in an static
method. Like changing static variables or calling static functions.
This answer could help too.
Difference between Static methods and Instance methods
Simply, a class method
("static method")is a property for the WHOLE class , so its's called as className.methodName()
.
BUT for instance methods
, then each object have their own copy of them, so they r called as objectName.instanceMethodName().
Second difference :
A third a bit advanced difference is that class (Static) methods are bound to the class at compile time
, while instance methods are bound to their objects at run time
..