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
I think you are confusing the use-case of static methods.
Of course you can use normal methods and static methods inside a class. The point of a static method is to be used outside of the class, without creating an object.
e.g.
classdef MfunEval
properties
Lm;
very_large_matrix;
end
properties (Dependent = true)
Data;
end
methods
function obj = MfunEval() %Constructor
obj.Lm = MfunEval.largeLim(); % this can be also non-static, doesn't matter, its INSIDE the object already.
obj.Lm = obj.largeLim(); % equivalent to above.
obj.very_large_matrix = zeros(10000000000000000000,100000000000000000);
end
end
methods (Static) %Static methods
function [lim]= largeLim()
lim=[-100, 100]
end
end
methods (Static) %Static methods
function [lim]= smallLim()
lim=[-2, 2]
end
end
end
For the example here, the fact that the method is static or not inside the constructor, its irrelevant. However, in your main code, you can now do
lim=MfunEval.smallLim() % does not construct an object, just calls the function
a=MfunEval; % Ouch! It has created a 1000000000000x10000000000 zeros matrix and you run out of memory
lim=a.smallLim() % works, but a needs to have been instantiated, so you provably ran out of memory.
In essence, static
methods have nothing to do with how you use them inside a class/object, and all about how you can use them outside the class.
Example of use: say you have a very complex class that takes as initialization 30 variables. You could make a static method called default_parameters()
that outputs the default values of these parameters, so the user can modify them, and then give them as an input to the constructor.
In general, a function of a class that does not need the values of the properties of a class can be a static method. Often, these are made to be static methods, not because you need them to be static, but because it makes conceptually sense that they are. Most of OOP is just about organizing your code in logical and structured ways anyway.