Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6852617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:24:53+00:00 2026-05-27T01:24:53+00:00

I frequently see a code snippet like this in class instance methods: static NSString

  • 0

I frequently see a code snippet like this in class instance methods:

static NSString *myString = @"This is a string.";

I can’t seem to figure out why this works. Is this simply the objc equivalent of a #define that’s limited to the method’s scope? I (think) I understand the static nature of the variable, but more specifically about NSStrings, why isn’t it being alloc’d, init’d?

Thanks~

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:24:54+00:00Added an answer on May 27, 2026 at 1:24 am

    Just stumbled upon the very same static NSString declaration. I wondered how exactly this static magic works, so I read up a bit. I’m only gonna address the static part of your question.

    According to K&R every variable in C has two basic attributes: type (e.g. float) and storage class (auto, register, static, extern, typedef).

    The static storage class has two different effects depending on whether it’s used:

    • inside of a block of code (e.g. inside of a function),
    • outside of all blocks (at the same level as a function).

    A variable inside a block that doesn’t have it’s storage class declared is by default considered to be auto (i.e. it’s local). It will get deleted as soon as the block exits. When you declare an automatic variable to be static it will keep it’s value upon exit. That value will still be there when the block of code gets invoked again.

    Global variables (declared at the same level as a function) are always static. Explicitly declaring a global variable (or a function) to be static limits its scope to just the single source code file. It won’t be accessible from and it won’t conflict with other source files. This is called internal linkage.

    If you’d like to find out more then read up on internal and external linkage in C.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I frequently find myself writing code like this: List<int> list = new List<int> {
I see this code quite frequently in some OSS unit tests, but is it
Been going over my predecessor's code and see usage of the request scope frequently.
I've been working on this for a while now and I can't seem to
Why is it that when I read others code I frequently see extensive use
In the source code of Activity.java, I see some methods bellow : public View
I frequently see python code similar to for line in open(filename): do_something(line) When does
In some code I've inherited, I see frequent use of size_t with the std
I frequently see, in particular in the PHP world, the following writing if you
Frequently, I've dug into apropos and docs looking for something like the following only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.