MATLAB: Different class definitions for value classes and a handle classes

handlesMATLABoop

Is there a difference in the class definition header for handle classes and value classes?
Also I have heard that the behaviour of handle classes is easier to understand for those familiar with Java. Is this true?

Best Answer

classdef MyClass < handle
is the obvious difference, but also your methods which make changes to the object of the class must return the object as an output argument if you are programming a value class (the default) e.g.
function obj = doSomething( obj )
and you must reassign back to the object.
I'm not a java programmer so I can't really answer that part, but handle classes work on a 'pass by reference' paradigm, value classes require this reassignment behaviour because they use more Matlab standard approach of copy by value. As a C++ programmer originally I certainly miss the lack of things like const references. A handle class passes around always like a non-const reference - as far as I am aware there is nothing that can change that, though obviously making properties private helps limit the possibilities for bugs as it always does.
There are other differences, handle classes can use events, for example, but these are covered in the documentation and the huge pdf that you can learn as you go along.
I use far more handle classes than value classes, but both can sting you if you don't use them correctly.
[SL: fixed typo. The definition of doSomething had an extra = between function and obj.]