ObjC: Initializing a Class Object

  • Thread starter Thread starter aychamo
  • Start date Start date
  • Tags Tags
    Class
Click For Summary

Discussion Overview

The discussion revolves around the initialization of class objects in Objective-C, specifically focusing on the use of static variables within the initialize method. Participants explore the behavior of static variables and their scope in the context of object-oriented programming.

Discussion Character

  • Exploratory, Technical explanation, Conceptual clarification

Main Points Raised

  • One participant questions the purpose of declaring a static variable initialized to NO within the initialize method, wondering if it would reset on subsequent calls.
  • Another participant suggests that in C and C++, static variable initialization occurs only once, implying that Objective-C may behave similarly.
  • A participant expresses confusion about how the code operates at runtime, particularly regarding the implications of declaring a static variable in the context of their previous experience with Pascal programming.
  • One participant asserts that declaring a variable as static places it on the heap rather than the stack, suggesting that its value persists across multiple invocations of the method.

Areas of Agreement / Disagreement

Participants exhibit uncertainty regarding the behavior of static variables in Objective-C, with differing understandings of their scope and lifetime. No consensus is reached on the specifics of how static variables function in this context.

Contextual Notes

There is a lack of clarity on the definitions and behaviors of static variables in Objective-C compared to other programming languages, as well as the implications of variable storage on the heap versus the stack.

Who May Find This Useful

Individuals learning about Objective-C, object-oriented programming concepts, or those transitioning from other programming languages may find this discussion relevant.

aychamo
Messages
375
Reaction score
0
Hello everyone;

I've been reading a document over on Apple's Developer website, called "The Object-C Language."

I'm reading about classes, and there is a part called "Initializing a Class Object" and it gives an example of how to implement an initialize method for a class:

PHP:
+ (void) initialize
{
     static BOOL initialized = NO;
     if (!initialized) {
          // Perform initialization here
          initialized = YES;
     }
}

So I assume at runtime you would send [myClass initialize]. What I don't understand, is in that method you declare the static variable initialized to = NO. If the initialize method got called again, wouldn't it set the initialized variable again to NO, and then rerun the initialization?

So my question is, what is the point of declaring the initialized = NO, and then checking if it's not initialaized? Would the initialize method be the only method able to access that initialized variable?

Sorry if this is a really basic question.. I'm trying to learn some basics of object oriented programming and objective-C from the docs.

Thank you
Aychamo
 
Technology news on Phys.org
In C and C++, when a static variable is initialized in this way, the assignment is only executed once. Perhaps the same is true of Object-C.
 
So how would the code operate at runtime? If the initialize method were called again, when it gets to the line:

static BOOL initialized = NO;

What would happen? I guess I'm stuck in my old thought of Pascal programming, where in a procedure/function any variable you declare there is only good for that one time the procedure/function is u sed.

I guess the question is, what, exactly, does "static" do to a variable?
 
aychamo said:
I guess the question is, what, exactly, does "static" do to a variable?
It puts in on the heap instead of the stack. While the stack is volatile, the heap is not. So when the routine is invoked a second time, the variable still holds the value it held when the last assignment was executed.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 25 ·
Replies
25
Views
6K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K