ObjC: Initializing a Class Object

  • Thread starter aychamo
  • Start date
  • Tags
    Class
In summary, the conversation discusses the use of a static variable in the initialize method for a class in Object-C. The purpose of the static variable is to only execute the assignment once, even if the initialize method is called multiple times. This is different from other programming languages where the variable would be re-initialized each time the method is called. The use of "static" puts the variable on the heap instead of the stack, making it non-volatile.
  • #1
aychamo
375
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
  • #2
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.
 
  • #3
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?
 
  • #4
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.
 

Related to ObjC: Initializing a Class Object

What is the purpose of initializing a class object in Objective-C?

Initializing a class object in Objective-C allows you to set initial values for the object's properties and perform any necessary setup before the object is used in your program.

How do you initialize a class object in Objective-C?

To initialize a class object in Objective-C, you use the class method + (instancetype)init or any custom initializer method defined in the class. You can also use the alloc and init methods together to allocate memory for the object and initialize it in one step.

Can you customize the initialization process for a class object in Objective-C?

Yes, you can create custom initializer methods in your class to customize the initialization process. These methods can take parameters to set specific values for the object's properties and perform any additional setup.

What happens if you don't initialize a class object in Objective-C?

If you don't initialize a class object in Objective-C, the object's properties will have default values and it may not be set up properly for use in your program. This can lead to unexpected behavior or crashes.

Can you initialize a class object multiple times in Objective-C?

Yes, you can initialize a class object multiple times in Objective-C. Each time you initialize it, the object's properties will be set to the values specified in the initialization process. However, if you have a custom initializer method, it may be a good idea to add a check to make sure the object is not already initialized before re-initializing it.

Similar threads

  • Programming and Computer Science
Replies
9
Views
917
  • Programming and Computer Science
Replies
25
Views
3K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
3
Views
786
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top