Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

OO Question: Static Properties?

63 views
Skip to first unread message

navels

unread,
Jun 22, 2008, 8:43:54 AM6/22/08
to
Static properties are mentioned in a few places in the Matlab
documentation, but don't seem to be explicitly documented themselves,
as far as I can see. They seem to work to a point, but the big
problem I am having is that I can't set them with a member function.

Here is some sample code:

classdef myClass

properties (Static)
x = 0 ;
end

methods (Static)
function f(val)
myClass.x = val ;
end
end

end

Then I can run:

>> myClass.x = 1 ;
>> myClass.x
ans = 1

So far, so good, but then

>> myClass.f(2) ;
>> myClass.x
ans = 1

What the heck?

Lee

Doug Schwarz

unread,
Jun 22, 2008, 1:51:42 PM6/22/08
to
In article
<a93a6633-deea-4593...@p25g2000hsf.googlegroups.com>,
navels <nav...@gmail.com> wrote:


What is a static property? Is it just the opposite of a dynamic
property, in other words, a normal property?

In any case, your function myClass.f doesn't have access to your
object's properties because you have defined it to be Static. All your
function does is implicitly define a new structure called myClass and
assign val to the x field. Then it doesn't return anything. Finally,
the way you have tried to use f() you'll need myClass to be a handle
class.

I think what you want is:

--------------- myClass.m ---------------------
classdef myClass < handle

properties
x = 0;
end

methods
function f(this,val)
this.x = val;
end
end

end
-----------------------------------------------


Also, in your use of this class you have mixed up the class name and an
instance of the class. The code should look something like this:

>> a = myClass;
>> a.x = 1;
>> a.x
ans =
1
>> a.f(2)
>> a.x
ans =
2

Is that the kind of thing you're trying to do? I still don't know what
adding the attribute Static to the properties will do since, as you say,
it is undocumented.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

navels

unread,
Jun 22, 2008, 5:45:45 PM6/22/08
to
On Jun 22, 12:51 pm, Doug Schwarz <s...@sig.for.address.edu> wrote:
[snip]

In OO programming, a static property is analogous to a static member
function. It is defined on the class instead of class instances.
That is why this code that I originally posted works:

-----------------------
>> myClass.x = 1 ;
>> myClass.x

ans = 1
-----------------------

I can set the property using the class name (first line) and access it
using the class name (second line), no instance required. This is
extremely useful, just as static member functions are extremely
useful. I would expect static member functions to be able to access
and modify static member data, as with most (all?) OO languages.

Lee

Doug Schwarz

unread,
Jun 22, 2008, 6:13:30 PM6/22/08
to
In article
<16e6d740-387f-4e78...@l42g2000hsc.googlegroups.com>,
navels <nav...@gmail.com> wrote:

Okay, I understand now, thanks. Given what you say and the fact that
you seem to be able to do it from the command line, I'd agree that your
static method should be able to set the static class property. I will
play with this myself and if I discover something useful I'll post again.

Maybe there's a reason that static properties are not documented. :-)

Doug Schwarz

unread,
Jun 22, 2008, 11:38:16 PM6/22/08
to
In article <see-90B820.1...@news.motzarella.org>,
Doug Schwarz <s...@sig.for.address.edu> wrote:

Lee,

I have had a chance to try out your code, but my results are not quite
the same as yours:

>> clear classes
>> myClass.x
ans =
0


>> myClass.x = 1;
>> myClass.x
ans =
1

>> myClass.f(2)
??? Reference to non-existent field 'f'.

>> class(myClass)
ans =
struct
>> clear classes
>> myClass.f(2)
>> myClass.x
ans =
0


I can access the x property of myClass, but when I try to run the f
method I find that myClass has been assigned as a structure. After
clearing everything again I am able to run f, but it behaves as you saw
and after inserting a disp(class(myClass)) statement in the f method I
see that, once again, myClass has become a struct in that workspace.

So, it seems that you cannot change the value of x in the class at all.
In fact, I don't see how it could work and remain consistent with MATLAB
syntax. At the moment when I execute myClass.x = 1, the workspace is
empty so MATLAB creates the myClass structure. It could work no other
way otherwise if someone tried to create a structure that happened to
have the same name as an existing class there would be a conflict.

Perhaps that's why static properties are undocumented.

navels

unread,
Jun 23, 2008, 11:26:29 AM6/23/08
to
Doug,

Regarding:

> It could work no other way otherwise if someone tried to create a structure that happened to have the same name as an existing class there would be a conflict.

This is already a problem with static member functions (as your
previous post shows), so really would be no different with static
properties. I guess I just need to accept that the implementation is
incomplete for now. It is clearly partially implemented, as I can use
the Static attribute with the properties keyword without getting an
error. And with the Static keyword, I can do a get (myClass.x), just
not a set (myClass.x = 1). Also, if I remove the Static keyword, I
can do this:

--------------------------------
>> myClass.x
??? No Static property 'x' in class 'myClass'.
--------------------------------

So MATLAB clearly knows what I am trying to do.

If anyone knows of a way to fake this out, please let me know.

Thanks,
Lee

Lee

Dave Foti

unread,
Jun 23, 2008, 12:13:10 PM6/23/08
to
In MATLAB, classes can define Constant properties, but not "static"
properties in the sense of other languages like C++. There were beta
releases that experimented with "Static" properties and the undocumented
attribute remains from then. However, the Static attribute is undocumented,
should not be used, and will likely be removed in a future MATLAB release.
R2008a implements it as a synonym for Constant and provides no additional
functionality beyond the documented behavior of Constant properties.

Constant properties may not be changed from the initial value specified in
the property declaration. There are a couple of reasons why MATLAB works
the way it does. First, MATLAB has longstanding rules that variables always
take precedent over the names of functions and classes and that assignment
statements introduce a variable if one doesn't already exist. Thus, any
expression of the form "A.B = C" will introduce a new variable A that is a
struct array containing a field B whose value is C. If "A.B = C" could
refer to a static property of class A, then class A would take precedent
over variable A and this would be a very significant incompatibility with
prior releases of MATLAB. It would mean that an m-file containing the
assignment statement "A.B = C" could have its meaning changed by the
introduction of a class named A somewhere on the MATLAB path. MATLAB
programmers have always been able to rely on assignment statements
introducing variables that shadow any other use of the same name.

Second, we have observed that static data is rarely used in other classes
except as private data within the class or as public constants. For
example, a survey of several Java class libraries found that all public
static fields were also final. In MATLAB, Constant properties can be used
like "public final static" fields in Java. For data internal to a class,
MATLAB already has persistent variables that can be created inside of
private or protected methods or local functions privately used by a class.
There are also good reasons to avoid static data in MATLAB where possible.
If a class has static data, it can be difficult to use the same class in
multiple applications because the static data can be a source of conflicts
among applications. In some other languages, this is less of an issue
because different applications are separately compiled into executables
running in different processes with different copies of class static data.
In MATLAB, frequently many different applications may be running in the same
process and environment with a single copy of each class.

-Dave Foti
d...@mathworks.com

"navels" <nav...@gmail.com> wrote in message
news:16e6d740-387f-4e78...@l42g2000hsc.googlegroups.com...

navels

unread,
Jun 23, 2008, 12:17:22 PM6/23/08
to
Dave,

Excellent, thanks for all the background info!

Lee

Larry Luther

unread,
Mar 30, 2009, 5:35:14 PM3/30/09
to
The description as to why "static" properties aren't supported is helpful, but the statement that they are not needed is self serving. The example of the singleton class with a "persistent" variable proves otherwise. Using Java as a model of a good object oriented language is nuts, go look at Smalltalk or Eiffel.

Doug

unread,
Apr 16, 2010, 1:50:25 PM4/16/10
to
navels <nav...@gmail.com> wrote in message <18915c18-6426-4cff...@x41g2000hsb.googlegroups.com>...

> Dave,
>
> Excellent, thanks for all the background info!
>
> Lee

This thread looks fairly stale, but it was the one that came up while I was searching, so I thought I would add my comment in the hope it will help others in the future.

I was also expecting to be able to have static properties, and had to find a work-around for what I was doing.

Here's a couple of things that I found one could do to get around this limitation.

As one option, the initial values don't have to be literals - you can call a function. See the "assigning values to constant properties" in the docs here: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/br4is5o-1.html

They use this example:

classdef NamedConst
properties(Constant)
RN = rand(5);
end
end

Unfortunately, this appears to only work with matlab intrinsic functions. In other words, I was unable to write my own function to do what I needed (not even a static one). Also, you can't change the value of RN at runtime.

In my case, the data member that I wanted to share across object instances was "kind of" "final". I didn't need to change the values at runtime, but I wanted all object instances to share a block of data. It was only created once, but that creation was computationally expensive, and the result was large. In other ways, the object is naturally a "value" object, but creating a copy of this "static" property for every instance was wasteful.

So here's the solution I used. I created a handle object that contained only the static property, then had a property in the main value object that contained a reference to the handle property. This way the static property could be shared (as a handle to an object) across instances, and only the handle was stored in the instances.

Note that this is, in effect, the Singleton pattern nicely shown here: http://www.mathworks.com.au/matlabcentral/fileexchange/24911

And reading the docs at the top of Simpleton.m, you see that they had to use the "persistent" keyword to work around the fact that "MATLAB OOP doesn't have the notion of static properties."

Anyway, here's the general mock up of what I did:

classdef myClassThatNeedsAStaticProperty
properties
referenceToHandleClass;
end
end

classdef HandleClassWithProperty < handle
properties
staticProperty;
end
end

This way I could create a single staticProperty (that was expensive to create and large) that was shared among instances of my value class.

After doing it this way, I realized that implementing it as a Singleton was actually a more elegant solution (in my situation) than a static property.

Hope this helps someone out there!

David Gutierrez

unread,
Feb 14, 2014, 6:39:08 PM2/14/14
to
navels <nav...@gmail.com> wrote in message <a93a6633-deea-4593...@p25g2000hsf.googlegroups.com>...
I hope this helps:

--------------------------------------

classdef myClass < handle

properties(Constant)
myClassInst=myClass;
end

properties
x
end

methods (Access = private)
function obj = myClass
obj.x = '';
end
end
methods(Static)
function f(val)
temp=myClass.myClassInst;
temp.x=val;
end
end
end

----------------------------------------------
>>p=myClass.myClassInst
>>p.x=1 % Temporary variable is needed for assignment, else it is interpreted as a struct
>> myClass.myClassInst.f(2)
>>myClass.myClassInst.x

The Doc helped me a lot:

http://www.mathworks.com/help/matlab/matlab_oop/properties-with-constant-values.html

Dave G

Abdulrahman Alabbasi

unread,
Mar 2, 2017, 5:41:07 AM3/2/17
to
Doug Schwarz <s...@sig.for.address.edu> wrote in message <see-087D7B.2...@news.motzarella.org>...
Thank you Doug for the interesting answer,

I have problem where at class 'S' I need to make changes to the properties of class 'A' (its even better if I don't have to initiate an instance of 'A'), these changes should be seen by other classes, e.g., 'B', 'C', and 'D', etc.

First, can I get these properties without making a new instance at each other class, like A.prop ?
Second, If I initiate and instance of class A at class S, is there anyway for other classes to access A's properties without me passing this instance to all classes?

Best Regards and thanks,
Abbasi

Doug Schwarz

unread,
Mar 2, 2017, 12:41:21 PM3/2/17
to
In article <o98srv$h7s$1...@newscl01ah.mathworks.com>,
"Abdulrahman Alabbasi" <alab...@kth.se> wrote:

[snip]

>
> Thank you Doug for the interesting answer,
>
> I have problem where at class 'S' I need to make changes to the properties of
> class 'A' (its even better if I don't have to initiate an instance of 'A'),
> these changes should be seen by other classes, e.g., 'B', 'C', and 'D', etc.

We have been admonished not to use static properties at all so I don't
see any way you could make changes to properties of a class without an
instance of that class, and in that case your changes would only apply
to that instance (object).

>
> First, can I get these properties without making a new instance at each other
> class, like A.prop ?
> Second, If I initiate and instance of class A at class S, is there anyway for
> other classes to access A's properties without me passing this instance to
> all classes?
>
> Best Regards and thanks,
> Abbasi

You might want to store your property values in a file or as a
preference item (see setpref and getpref), and then use a static
function of class A to access the data. Just a suggestion.
0 new messages