A value class constructor returns an instance that is associated with the variable to which it is assigned. If you reassign this variable, MATLAB® creates a copy of the original object. If you pass this variable to a function, the function must return the modified object.
A handle class constructor returns a handle object that is a reference to the object created. You can assign the handle object to multiple variables or pass it to functions without causing MATLAB to make a copy of the original object. A function that modifies a handle object passed as an input argument does not need to return the object.
All handle classes must subclass the abstract
Modifying Objects compares handle and value object behavior when used as arguments to functions.
MATLAB support two kinds of classes — handle classes and value classes. The kind of class you use depends on the desired behavior of the class instances and what features you want to use.
Use a handle class when you want to create a reference to the data contained in an object of the class, and do not want copies of the object to make copies of the object data. For example, use a handle class to implement an object that contains information for a phone book entry. Multiple application programs can access a particular phone book entry, but there can be only one set of underlying data.
The reference behavior of handles enables these classes to support features like events, listeners, and dynamic properties.
Use value classes to represent entities that do not need to be unique, like numeric values. For example, use a value class to implement a polynomial data type. You can copy a polynomial object and then modify its coefficients to make a different polynomial without affecting the original polynomial.
Which Kind of Class to Use describes how to select the kind of class to use for your application.
If you create an object of the class
make a copy of this object, the result is two independent objects
having no data shared between them. The following code example creates
an object of class
int32 and assigns it to variable
and then copies it to
b. When you raise
the fourth power and assign the value again to the variable
a, MATLAB creates
an object with the new data and assigns it to the variable
overwriting the previous assignment. The value of
a = int32(7); b = a; a = a^4; b 7
MATLAB copies the value of
which results in two independent versions of the original object.
This behavior is typical of MATLAB numeric classes.
Handle Graphics® classes return a handle to the object created. A handle is a variable that references an instance of a class. If you copy the handle, you have another variable that refers to the same object. There is still only one version of the object data. For example, if you create a Handle Graphics line object and copy its handle to another variable, you can set the properties of the same line using either copy of the handle.
x = 1:10; y = sin(x); h1 = line(x,y); h2 = h1; set(h2,'Color','red') % line is red set(h1,'Color','green') % line is green delete(h2) set(h1,'Color','blue')
MATLAB returns an
Invalid or deleted object.
error in this case.
If you delete one handle, all copies are now invalid because you have deleted the single object to which all copies point.
Value class instances behave like built-in numeric classes and handle class instances behave like Handle Graphics objects, as illustrated in Behavior of MATLAB Built-In Classes.
MATLAB associates objects of value classes with the variables
to which you assign them. When you copy a value object, MATLAB also
copies the data contained by the object. The new object is independent
of changes to the original object. Instances behave like standard MATLAB numeric
struct classes. Each property behaves essentially
like a MATLAB array See Memory Allocation for Arrays for more information.
Use value classes when assigning an object to a variable and passing an object to a function must make a copy of the function. Value objects are always associated with one workspace or temporary variable and go out of scope when that variable goes out of scope or is cleared. There are no references to value objects, only copies which are themselves objects.
For example, suppose you define a
Coefficients property stores the coefficients
of the polynomial. Note how copies of these value-class objects are
independent of each other:
p = polynomial([1 0 -2 -5]); p2 = p; p.Coefficients = [2 3 -1 -2 -3]; p2.Coefficients ans = 1 0 -2 -5
All classes that are not subclasses of the
are value classes. Therefore, the following
a value class named
Objects of handle classes use a handle to reference objects of the class. A handle is a variable that identifies an instance of a class. When you copy a handle object, MATLAB copies the handle, but not the data stored in the object properties. The copy refers to the same data as the original handle. If you change a property value on the original object, the copied object reflects the same change.
All handle classes are subclasses of the abstract
In addition to providing handle copy semantics, deriving from the
enables your class to:
Inherit a number of useful methods (Handle Class Methods)
Define events and listeners (Events and Listeners — Syntax and Techniques)
Define dynamic properties (Dynamic Properties — Adding Properties to an Instance)
Implement Handle Graphics type set and get methods (Implementing a Set/Get Interface for Properties)
handle class explicitly to create
a handle class:
classdef myClass < handle
See The Handle Superclass for more information on the handle class and its methods.
If you subclass a class that is itself a subclass of the
your subclass is also a handle class. You do not need to specify the
handle superclass explicitly in your class definition. For example,
employee class is a subclass of the handle
classdef employee < handle ... end
Create a subclass of the
employee class for
engineer employees, which is also a handle class. You do not need
handle as a superclass in the
classdef engineer < employee ... end
A handle is an object that references its data indirectly. When constructing a handle, the MATLAB runtime creates an object with storage for property values and the constructor function returns a handle to this object. When you assign the handle to a variable or when you pass the handle to a function, MATLAB copies the handle, but not the underlying data.
For example, suppose you have defined a
that stores data about company employees, such as the department in
which they work:
classdef employee < handle properties Name = '' Department = ''; end methods function e = employee(name,dept) e.Name = name; e.Department = dept; end % employee function transfer(obj,newDepartment) obj.Department = newDepartment; end % transfer end end
transfer method in the previous code
changes the employee's department (the
employee object). In the following statements,
a copy of the handle object
e. Notice that when
you change the
Department property of object
the property value also changes in object
e = employee('Fred Smith','QE'); e2 = e; % Copy handle object transfer(e,'Engineering') e2.Department ans = Engineering
e2 is an alias for
refers to the same property data storage as
See Initializing Property Values for information on the differences between initializing properties to default values in the properties block and initializing properties from within the constructor. Also, see Initialize Arrays of Handle Objects for related information on working with handle classes.
employee class was a value class,
transfer method would modify only its
local copy of the
employee object. In value classes,
transfer that modify the object must
return a modified object to copy over the existing object variable:
function obj = transfer(obj,newDepartment) obj.Department = newDepartment; end
When you call
transfer, assign the output
argument to create the modified object.
e = transfer(e,'Engineering');
In a value class, the
transfer method does
not affect the variable
e2, which is a different
employee object. In this example, having two independent copies of
objects representing the same employee is not a good design. Hence,
employee class as a handle class.
You can destroy handle objects before they become unreachable
by explicitly calling the
Deleting the handle of a handle class object makes all handles invalid.
delete(e2) e.Department Invalid or deleted object.
delete function on a handle object
invokes the destructor function or functions for that object. See Handle Class Destructor for more information.