• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Apples's code examples... It is me
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Apples's code examples... It is me


  • Subject: Re: Apples's code examples... It is me
  • From: Jean-Daniel Dupas <email@hidden>
  • Date: Wed, 6 Feb 2008 15:55:18 +0100


Le 6 févr. 08 à 15:10, Yanko Ivanov a écrit :

Kyle, I did notice the "static" keyword. However the it is still
confusing for me (Java programmer).

Stéphane, yes it is me.

I should have made a test (http://pastie.textmate.org/148207) before posting :)

Regards,
Yanko

if you come from the java world, you probably know the singleton pattern:


class MyClass {

static MyClass _sharedInstance;

public static MyClass sharedInstance() {
	if (_sharedInstance)
		return _sharedInstance;

	/* put multithread support here (synchronize(), etc...) */
	_sharedInstance = new MyClass();

	return _sharedInstance;
}

	protected MyClass() {
		/* protected constructor */
	}
}

In the Apple sample code, this is the same pattern, but static is not exactly the same in C than in Java. a static variable is stored in the heap and does not each from one call to another.
a Static variable initialization is perform only one time, so the "static id myObject = nil;" is not perform on each call by only once at program startup.


(From Wikipedia: In the C programming language, static is used with global variables and functions to set their scope to the containing file. In local variables, static is used to store the variable on the heap instead of in the transient call stack.)_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Apples's code examples... It is me
      • From: "Yanko Ivanov" <email@hidden>
References: 
 >Re: Apples's code examples... It is me (From: "Yanko Ivanov" <email@hidden>)

  • Prev by Date: Re: Safari bookmarks...
  • Next by Date: Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful
  • Previous by thread: Re: Apples's code examples... It is me
  • Next by thread: Re: Apples's code examples... It is me
  • Index(es):
    • Date
    • Thread