Static methods are unnecessary, right?

14 views (last 30 days)
fa wu
fa wu on 9 Aug 2023
Edited: James Tursa on 8 Dec 2023
I researched static methods and feel that static methods are non-essential in matlab.There would be no inconvenience if matlab did not provide a static method.I wonder if my above opinion is correct?
The benefit of static methods is "not related to the specific object", only related to classes.Can be called inside the class.Also can be called by the object!
I found I can do what the static method does with ordinary methods!
Here's a simple example.method1 didn't use input argument. I can use method1 in constructor.Although the program uses 'obj.method1' to call method1, this does not mean that method1 is related to any specific object.
classdef test<handle
properties
X
Y
end
methods
function obj = test(inputArg1,inputArg2)
obj.X = inputArg1 + inputArg2;
obj.Y=obj.method1(inputArg2);
end
end
methods
function value = method1(obj,inputArg)
value = 3 + 3;
end
end
end
Here's a more complex example.Use object-oriented programming method to draw three-dimensional surface graph. It is required to define two classes, one of which stores the data of the function, and uses a static method to calculate the grid coordinates of the interval in this class; the function of the other class is to draw a three-dimensional surface map, and the drawing process is realized by a static method.
@MfunEval/MfunEval.m
classdef MfunEval
properties
HFun;
Lm = [-2*pi 2*pi];
end
properties (Dependent = true)
Data;
end
methods
function obj = MfunEval(fun_handle,limits) %Constructor
obj.HFun = fun_handle;
obj.Lm = limits;
end
function data = get.Data(obj) %
[x,y] = MfunEval.grid(obj.Lm);
z= obj.HFun(x,y);
data.X = x;
data.Y = y;
data.Z = z;
end
end
methods (Static) %Static methods
function [x,y] = grid(lim)
step = (lim(2)-lim(1))/50;
[x,y] = meshgrid(lim(1):step:lim(2));
end
end
end
@MfunView/MfunView.m
classdef MfunView
properties
FunObj
HSurf
end
methods
function obj = MfunView(fobj) %Constructor
obj.FunObj= fobj;
end
end
methods (Static = true) %
createViews(a) %
end
end
@MfunView/ createViews.m
function createViews(funevalobj)
viewobj=MfunView(funevalobj);
viewobj.HSurf = surf(viewobj.FunObj.Data.X,...
viewobj.FunObj.Data.Y,...
viewobj.FunObj.Data.Z);
shading interp;
end
I modified @MfunEval/MfunEval.m Replaced MfunEval.grid with obj.grid.I change the static method to a normal method. The program can still run!
classdef MfunEval
properties
HFun;
Lm = [-2*pi 2*pi];
end
properties (Dependent = true)
Data;
end
methods
function obj = MfunEval(fun_handle,limits) %Constructor
obj.HFun = fun_handle;
obj.Lm = limits;
end
function data = get.Data(obj)
%[x,y] = MfunEval.grid(obj.Lm);
[x,y] = obj.grid(obj.Lm); %call grid function
z= obj.HFun(x,y);
data.X = x;
data.Y = y;
data.Z = z;
end
end
methods %(Static) %change Static
function [x,y] = grid(obj,lim)
step = (lim(2)-lim(1))/50;
[x,y] = meshgrid(lim(1):step:lim(2));
end
end
end

Answers (2)

James Tursa
James Tursa on 8 Dec 2023
Edited: James Tursa on 8 Dec 2023
I use Constant Properties and Static Methods all the time in classes I am creating. In particular, when I am developing a group of functions that are all interrelated I will create a class to hold them all. Creating an object of this class is an unnecessary step since all methods are designed to be called using the class name directly. The constant properties can be accessed in the same manner. In fact, creating an object of this class serves no purpose since the class has no properties that are non-constant. I find Static Methods a very useful feature.

Chi
Chi on 8 Dec 2023
Edited: Chi on 8 Dec 2023
Yes, you can modify MfunEval class to run by changing the static grid() method to an ordinary method for the MfunEval class defined above.
However, the first version of MfunEval (static grid) would enable grid() to be called without an instance of MfunEval class, via:
MfunEval.grid([0 0])
whereas the second version of MfunEval would not.
You'll still need to define grid() as a static method if you would like to use the same grid method in multiple classes.
@doc:https://www.mathworks.com/help/matlab/matlab_oop/static-methods.html

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!