Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: OO Question: Static Properties?

Subject: OO Question: Static Properties?

From: navels

Date: 22 Jun, 2008 12:43:54

Message: 1 of 8

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

Subject: OO Question: Static Properties?

From: Doug Schwarz

Date: 22 Jun, 2008 17:51:42

Message: 2 of 8

In article
<a93a6633-deea-4593-84fb-84555631efbc@p25g2000hsf.googlegroups.com>,
 navels <navels@gmail.com> wrote:

> 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


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.

Subject: OO Question: Static Properties?

From: navels

Date: 22 Jun, 2008 21:45:45

Message: 3 of 8

On Jun 22, 12:51=A0pm, 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 =3D 1 ;
>> myClass.x

ans =3D 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

Subject: OO Question: Static Properties?

From: Doug Schwarz

Date: 22 Jun, 2008 22:13:30

Message: 4 of 8

In article
<16e6d740-387f-4e78-8202-41e00ce5d0b7@l42g2000hsc.googlegroups.com>,
 navels <navels@gmail.com> wrote:

> 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

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
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: OO Question: Static Properties?

From: Doug Schwarz

Date: 23 Jun, 2008 03:38:16

Message: 5 of 8

In article <see-90B820.18133022062008@news.motzarella.org>,
 Doug Schwarz <see@sig.for.address.edu> wrote:

> In article
> <16e6d740-387f-4e78-8202-41e00ce5d0b7@l42g2000hsc.googlegroups.com>,
> navels <navels@gmail.com> wrote:
>
> > 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
>
> 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. :-)

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.

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

Subject: OO Question: Static Properties?

From: navels

Date: 23 Jun, 2008 15:26:29

Message: 6 of 8

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

Subject: OO Question: Static Properties?

From: Dave Foti

Date: 23 Jun, 2008 16:13:10

Message: 7 of 8

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
daf@mathworks.com

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

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.


Subject: OO Question: Static Properties?

From: navels

Date: 23 Jun, 2008 16:17:22

Message: 8 of 8

Dave,

Excellent, thanks for all the background info!

Lee

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
undocumented Yair Altman 27 Jul, 2008 15:44:36
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics