<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Java Developer&#8217;s Guide to Static variables in Objective-C</title>
	<atom:link href="http://MobileDeveloperTips.com/objective-c/java-developers-guide-to-static-variables-in-objective-c.html/feed" rel="self" type="application/rss+xml" />
	<link>http://MobileDeveloperTips.com/objective-c/java-developers-guide-to-static-variables-in-objective-c.html</link>
	<description>iOS and Objective-C Tips, Tricks and Tutorials.</description>
	<lastBuildDate>Tue, 22 May 2012 11:53:47 -0500</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Lalit</title>
		<link>http://MobileDeveloperTips.com/objective-c/java-developers-guide-to-static-variables-in-objective-c.html#comment-67878</link>
		<dc:creator>Lalit</dc:creator>
		<pubDate>Fri, 02 Mar 2012 05:24:56 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=814#comment-67878</guid>
		<description>Thanks for this nice and simple explanation!</description>
		<content:encoded><![CDATA[<p>Thanks for this nice and simple explanation!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Neal</title>
		<link>http://MobileDeveloperTips.com/objective-c/java-developers-guide-to-static-variables-in-objective-c.html#comment-12372</link>
		<dc:creator>Steve Neal</dc:creator>
		<pubDate>Wed, 31 Mar 2010 18:02:02 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=814#comment-12372</guid>
		<description>Nice article - helpful too, thanks.</description>
		<content:encoded><![CDATA[<p>Nice article &#8211; helpful too, thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Markus Gasser</title>
		<link>http://MobileDeveloperTips.com/objective-c/java-developers-guide-to-static-variables-in-objective-c.html#comment-2423</link>
		<dc:creator>Markus Gasser</dc:creator>
		<pubDate>Tue, 26 May 2009 06:30:30 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=814#comment-2423</guid>
		<description>@Cookiecat: It won&#039;t. But you wouldn&#039;t want that anyway since it is a static variable and thus has a lifetime over the whole process. If you do know that you need the variable only to some specific point, you can always release it manually.

Perphaps an addition to the above example: If you want to have a default value for your variable you can override the + (void) initialize; or + (void) load; method (Refer to the documentation of NSObject to know which one to use when), which are called before the class is used.</description>
		<content:encoded><![CDATA[<p>@Cookiecat: It won&#8217;t. But you wouldn&#8217;t want that anyway since it is a static variable and thus has a lifetime over the whole process. If you do know that you need the variable only to some specific point, you can always release it manually.</p>
<p>Perphaps an addition to the above example: If you want to have a default value for your variable you can override the + (void) initialize; or + (void) load; method (Refer to the documentation of NSObject to know which one to use when), which are called before the class is used.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cookiecat</title>
		<link>http://MobileDeveloperTips.com/objective-c/java-developers-guide-to-static-variables-in-objective-c.html#comment-283</link>
		<dc:creator>Cookiecat</dc:creator>
		<pubDate>Mon, 09 Mar 2009 00:19:48 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=814#comment-283</guid>
		<description>Thanks for the helpful article.  I am wondering how and when myVar gets released in your example.</description>
		<content:encoded><![CDATA[<p>Thanks for the helpful article.  I am wondering how and when myVar gets released in your example.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Wetherill</title>
		<link>http://MobileDeveloperTips.com/objective-c/java-developers-guide-to-static-variables-in-objective-c.html#comment-66</link>
		<dc:creator>Steve Wetherill</dc:creator>
		<pubDate>Sun, 19 Oct 2008 07:05:11 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=814#comment-66</guid>
		<description>The use of #define (ie, C preprocessor macros) is powerful, but very easy to abuse/misuse. The use of #define for constants is probably one of the safest uses of it, but when you take it further it is very easy to introduce side effects. Consider this:

#define MULT(a, b) a * b

Which on the face of it is a simple macro that will return the product of two numbers. Then, consider this use of the macro:

int x = 2;
int y = MULT(x, x + 1);

You might expect y to be 6 when this runs, but actually you&#039;ll get 5 because the macro expands to:

int y = x * x + 1;

This is pretty standard stuff, but it is something I can see Java coders tripping up over. It is important to realize that the preprocessor is just dealing with text, and the compiler gets what the preprocessor spits out, which in this case is not what you would expect.

On the topic of constants, there are at least a couple of other ways to define them. On a global basis you can use tyepdef enum, in a header file, to define constants that are available outside of your Obj-C class. So for example in your .h file, before your interface definition, you could put:

// Graphics.h

	typedef enum {
		DOTTED = 1,
		SOLID = 0,
	} GraphicsStyle;

Now, any file that #imports Graphics.h can do:

GraphicsStyle gs = DOTTED;

In this case, gs is a variable of the enum type GraphicsStyle, and so you can do things like:

if (gs == DOTTED) {
//
}

And so on. enums can be assigned to ints and vice versa, but there are some safety issues to consider. Google &quot;C enums&quot; for more info! One thing to note is that enums are not namespaced by the typedef, so this will give a duplicate symbol warning on VALUE:

typedef enum {
    VALUE = 1,
} SomeValues;

typedef enum {
   VALUE = 1,
} SomeOtherValues;

This is why you&#039;ll see the following used in Obj-C:

typedef enum {
   UIStatusBarStyleGray
   UIStatusBarStyleTransparentBlack,
   UIStatusBarStyleOpaqueBlack
} UIStatusBarStyle;

One other thing to note here is that enums are auto-incrementing, and the first value in the enum is set to 0 by default. Hence, in the UIStatusBarStyle enum above, UIStatusBarStyleGray will have value 0, and each succeeding enum element will have a value incremented by 1.

Hopefully all the above info is correct, insert usual disclaimers, etc. I&#039;m much more familiar with C++ and like Rodney I&#039;m finding my way w/Obj-C albeit coming from a different angle.</description>
		<content:encoded><![CDATA[<p>The use of #define (ie, C preprocessor macros) is powerful, but very easy to abuse/misuse. The use of #define for constants is probably one of the safest uses of it, but when you take it further it is very easy to introduce side effects. Consider this:</p>
<p>#define MULT(a, b) a * b</p>
<p>Which on the face of it is a simple macro that will return the product of two numbers. Then, consider this use of the macro:</p>
<p>int x = 2;<br />
int y = MULT(x, x + 1);</p>
<p>You might expect y to be 6 when this runs, but actually you&#8217;ll get 5 because the macro expands to:</p>
<p>int y = x * x + 1;</p>
<p>This is pretty standard stuff, but it is something I can see Java coders tripping up over. It is important to realize that the preprocessor is just dealing with text, and the compiler gets what the preprocessor spits out, which in this case is not what you would expect.</p>
<p>On the topic of constants, there are at least a couple of other ways to define them. On a global basis you can use tyepdef enum, in a header file, to define constants that are available outside of your Obj-C class. So for example in your .h file, before your interface definition, you could put:</p>
<p>// Graphics.h</p>
<p>	typedef enum {<br />
		DOTTED = 1,<br />
		SOLID = 0,<br />
	} GraphicsStyle;</p>
<p>Now, any file that #imports Graphics.h can do:</p>
<p>GraphicsStyle gs = DOTTED;</p>
<p>In this case, gs is a variable of the enum type GraphicsStyle, and so you can do things like:</p>
<p>if (gs == DOTTED) {<br />
//<br />
}</p>
<p>And so on. enums can be assigned to ints and vice versa, but there are some safety issues to consider. Google &#8220;C enums&#8221; for more info! One thing to note is that enums are not namespaced by the typedef, so this will give a duplicate symbol warning on VALUE:</p>
<p>typedef enum {<br />
    VALUE = 1,<br />
} SomeValues;</p>
<p>typedef enum {<br />
   VALUE = 1,<br />
} SomeOtherValues;</p>
<p>This is why you&#8217;ll see the following used in Obj-C:</p>
<p>typedef enum {<br />
   UIStatusBarStyleGray<br />
   UIStatusBarStyleTransparentBlack,<br />
   UIStatusBarStyleOpaqueBlack<br />
} UIStatusBarStyle;</p>
<p>One other thing to note here is that enums are auto-incrementing, and the first value in the enum is set to 0 by default. Hence, in the UIStatusBarStyle enum above, UIStatusBarStyleGray will have value 0, and each succeeding enum element will have a value incremented by 1.</p>
<p>Hopefully all the above info is correct, insert usual disclaimers, etc. I&#8217;m much more familiar with C++ and like Rodney I&#8217;m finding my way w/Obj-C albeit coming from a different angle.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

