MATLAB: Are properties of unique objects referring to the same object

handle objectMATLAB

I am confused on how to use handle objects as properties in matlab. For example, I defined the following classes:
classdef Page < handle
properties
word1;
word2;
end
classdef Book < handle
properties
page1 = Page;
page2 = Page;
end
Now I instantiate two books:
iliad = Book;
odyssey = Book;
If I check if iliad and odyssey are the same:
eq(iliad, odyssey)
I get:
ans = logical 0
So far so good. But if I check if page1 of iliad and odyssey are the same:
eq(iliad.page1, odyssey.page1)
I get:
ans = logical 1
This is not good! It means that if I change page1 of odyssey, page1 of iliad will also change. What am I misunderstanding? How do I deal with this issue?

Best Answer

This is expected behavior. See the "Initialize Properties to Unique Values" section on this documentation page. Initialize the page1 and page2 properties of the Book object in the constructor if you want them not to refer to the same handle.