ObjC: Initializing a Class Object

  • Thread starter Thread starter aychamo
  • Start date Start date
  • Tags Tags
    Class
AI Thread Summary
The discussion centers around the use of the `initialize` method in Objective-C and the behavior of static variables within that context. The example provided demonstrates how to implement an `initialize` method with a static variable `initialized`. The key point is that the static variable retains its value between method calls, meaning it is initialized only once, regardless of how many times the `initialize` method is invoked. This is in contrast to local variables, which are reset each time a method is called. The static keyword ensures that the variable is stored in a persistent memory area (the heap), allowing it to maintain its state across multiple calls to the method. This behavior is crucial for ensuring that initialization code runs only once, preventing redundant operations and potential errors in object-oriented programming.
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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top