4

Since MATLAB does not provide self-reference, what is the actual difference between a static and a non-static method in MATLAB, apart from the latter not being call-able without a class instance? One always has to pass a reference to the object-to-be-modified anyway (edit apart from setters, getters and overloaded operators which implicitly include the self-reference)

Community
  • 1
  • 1
Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221

2 Answers2

5

For non-static methods, Matlab provides the calling class as the first argument. By (personal convention) I call this argument self, which then emulates a self reference syntax. e.g.:

methods (Static = false)
    function output = someMethod(self, arg1, arg2, arg3)
        self.x      %Now refers to the (potentially private) field `x`
        self.someOtherFunction(arg1, arg2) %Calls another method, which may be static or not.
    end
end

By contrast

methods (Static = true)
    function output = someStaticMethod(arg1, arg2, arg3)
        %There is no input appropriate to the name "self" 
        someOtherFunction(arg1, arg2) %Calls another method, which must be static
    end
end

Given an object someObject, these methods can both be called by using:

someObject.someMethod(arg1, arg2, arg3)
someObject.someStaticMethod(arg1, arg2, arg3)

The self-reference discussed in the linked question is referring to package names, which is an entirely different animal.

Gastón Bengolea
  • 855
  • 6
  • 13
Pursuit
  • 12,285
  • 1
  • 25
  • 41
  • 1
    ah, I misunderstood this, so MATLAB _does_ have some kind of `this` object, only it is explicitly named in the method definition (but _not_ passed in the function call). Thanks! Now this does actually make sense – Tobias Kienzler Mar 07 '12 at 14:35
  • 1
    It can be passed in the function call, depending on whether you call it using object or functional syntax. Using @Pursuit's example, you could type either `someObject.someMethod(arg1, arg2, arg3)' or someMethod(someObject, arg1, arg1, arg3)'. Both are typically equivalent, and the equivalence maybe illuminates why MATLAB method signatures are as they are. – Sam Roberts Mar 07 '12 at 16:03
  • Those calling forms are typically equivalent, but it's the case where they aren't that probably explains why there's no implicit `this`. If you use the dotless `someMethod(someObject, arg1, arg2)` syntax, the method dispatch object doesn't have to be the first argument. You could call `someMethod(arg1, arg2, someObject)` instead. The method called depends on the runtime types of all the arguments, and their superiority relationships. With multiple dispatch like this, there's not necessarily a single method dispatch object that would be used for an implicit `this`. – Andrew Janke Mar 07 '12 at 17:26
0

Perhaps more relevant is that static methods can be called without invoking the objects constructor: i.e., if class foo has static method bar, then foo.bar() invokes static method bar without the constructor foo() ever having been invoked.

lsfinn
  • 446
  • 2
  • 4