Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Question about Mata classes and Static keyword

From   Erik Lindsley <>
Subject   st: Question about Mata classes and Static keyword
Date   Wed, 01 Feb 2012 09:39:59 -0600

  I hope I am posting to the right list about my Stata/Mata programming question. If there is a another list I should be using please just let me know.

 I am trying to use the static keyword with a Mata class object, but my _static_ variables seem to disappear when accessed from inside a _static_ function.

It seems that from within _static_ functions, NO outside variables can be accessed, including other _static_ class variables?!?
Perhaps I am not understanding the semantics of the static keyword, as this behavior seems to be a departure from the "static" keyword found in other programming languages (such as C/C++)?

For the record, I am running the latest binary (01 Sep 2011, according to update query) of StataSE 11.2.

Any help would be greatly appreciated...
 - Erik Lindsley

P.S.: Here is a simple test case to illustrate my problem, along with the output....

 class STest {
 	private static real scalar s 
 	void setLocal()
 	real scalar getLocal()
 	static void setStatic()
 	static real scalar getStatic()
 	void STest::setLocal(real scalar val) {
 	real scalar STest::getLocal() {

 	void STest::setStatic(real scalar val) {
 	real scalar STest::getStatic() {

void runTest() {
	class STest scalar ST
	printf("STest::getLocal returned %f  (-1 was expected)\n",ST.getLocal());
	printf("STest::getStatic returned %f  (-1 was expected)\n",STest::getStatic());

Output (fails at run time)....
: runTest()
STest::getLocal returned -1  (-1 was expected)
      STest::getStatic():  3204  s[0,0] found where scalar required [2]
               runTest():     -  function returned error [5]
                 <istmt>:     -  function returned error [1]


*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index