Expression Evaluation in Handle and Value Classes
The following example shows how value and handle object behave when assigned to
properties as default values. Suppose that you have the following classes.
Expressions in Value Classes
The ClassExp class has a property that contains a ContClass object:
classdef ContClass
properties
% Assign current date and time
TimeProp = datestr(now)
end
end
classdef ClassExp
properties
ObjProp = ContClass
end
end
When you first use the ClassExp class, MATLAB creates an instance of the ContClass
class. MATLAB initializes both classes at this time. All instances of ClassExp include a
copy of this same instance of ContClass.
a = ClassExp;
a.ObjProp.TimeProp
ans =
08-Oct-2003 17:16:08
The TimeProp property of the ContClass object contains the date and time when
MATLAB initialized the class. Creating additional instances of the ClassExp class shows
that the date string has not changed:
b = ClassExp;
b.ObjProp.TimeProp
ans =
08-Oct-2003 17:16:08
6 Defining and Organizing Classes