I already understand that static in objective-c is different from static in say Java. My question is for Objective-c static variables. I have a static variable in Objective-C. If it is set in one instance of a class. Will the value be visible if I were to create a new instance of the class after the variable was previously set?
Yes, that's the point of static variables. They are not instance variables. A static variable exists just once in its scope. It is initialized once as well.
This is true for static variables declared outside of any method as well as static variables declared within a method.
The static keyword is merely used to limit the scope of a variable so that it can only be referenced directly within its implementation file.
The static keyword is commonly used with global variables to implement Objective-C's half-baked class variables, so yes, you are correct, that a single instance of the variable will exist regardless of the number of instances of the class (including zero), and that's basically because it's completely unrelated to the class.
Related
I know that it is possible in Objective C to define an instance variable without a property when I use curly braces in the top of the interface or the implementation. But what is the advantage when I do that?
I think your question should be comparing to property what is the advantage of defining instance variables.
Most property is backed by a instance variables. By default, a readwrite property will be backed by an instance variable, which will be synthesized automatically by the compiler. (A readonly property won't synthesize an instance variable, it only synthesizes a getter method which a readwrite property synthesize an instance variable and a getter and a setter method)
So property provides methods that synthesized by compiler to access the instance variable. I can't say which one has any advantages, just use the one which is proper to your case.
You may need to learn more about property. Properties Encapsulate an Object’s Values
The most advantage is this variables actually can be private/protected/public members according to the keyword (#private/#protected/#public) you set.
Take a look at this question
Since Objective-C doesn't support const instance variables in classes what are some alternatives to ensure the value of the variable doesn't get changed? I do not want to resort to preprocessor #defines because I would prefer class variables. Also, is there a reason const instance variables aren't supported?
Objects in Objective-C are constructed differently than those in C++ or Java. All instance variables are initialized to zero by the alloc method, before the init method is called, so it would be too late by the time init is called to change a const instance variable. Obviously the compiler writers could modify the compiler to support changing a const instance variable in an init method, but they haven't done so.
Typically you just make your instance variables #private and expose them using accessor methods. If you don't want an instance variable's value to change, don't expose a setter method and don't modify the variable in your class implementation.
If you drop support for 32-bit Mac OS X, you can put your instance variables in your #implementation instead of your #interface, which completely protects them from meddling by other classes.
Is it possible to share one parameter of a class among all the instances of this class, in objective-c?:
#interface Class1 : NSObject {
NSString* shared; /** shared among instance (this is not, but defined somehow) **/
NSString* non_shared; /** other parameters non shared **/
}
In the program then, each instance of Class1 has its own non_shared variables (as usual), but all access the same shared one (when one instance changes it all can see it).
One possibility is to hide the variable as a property and use a singleton in the setter/getter functions, but I don't know if there is a simple way.
Thanks,
Edu
Class variables (called static in many other OOP languages) are actually a bit of a pain in Objective-C. You have to declare a static global variable in the class' module (.m) file and reference that variable. You should add class-level getter/setters to encapsulate access to the static global variable. Your getter can alloc/init an object and put it in the variable if it is uninitialized before returning it.
If the static variable holds an instance (e.g. an NSString instance in your example), you need to make sure it doesn't get alloc/initialized more than once. Take a look at dispatch_once if you're on OS X 10.6 or greater to guarantee single initialization.
I know that class variables are declared in memory (as opposed to on the stack) when the class is initialized, and I know how class methods are basically used. But I have some questions about class methods that aren't answered in the basic documentation.
Are class method also declared in memory? What about any object declared within these class methods? Are they 'static' in scope? What about any objects that are passed into a class method as parameter? Are those also 'static'?
Does repeatedly calling a class method mean all the objects declared within it are allocated again and again (one per method call), or are they living in one location in memory? Do they get cleared at every run?
For example, what happens to the do_something method here:
+ (void) main
{
while (i < MAX)
{
[MyClass do_something];
}
}
+ (void) do_something
{
NSMutableArray *array = [[NSMutableArray alloc] init];
...
[array release];
}
Class methods follow the same rules as object (instance) methods except you cannot access instance variables from class methods, obviously because ivars get allocated per object instance.
In your example "array" is allocated on heap with each call, as usual.
All variables are stored "in memory", no matter their storage type (static, automatic, free store), location (stack or heap), linkage or scope. A variable is static only if it's declared static. Otherwise, variables in class methods, whether parameters or local variables, have function or local scope, automatic storage, no linkage and are stored on the stack.
Class methods have global scope and external linkage, though you can send a message to an object (including classes) even if there isn't a handler in scope. Internal linkage should be possible, but I don't think the language supports declaring methods with internal linkage. Storage type and location doesn't really apply to methods, but you could say methods have static storage.
When calling the +(void) do_something method the array object will be initialised, as your code specifies, every time. It is only declared the scope of that method.
You can declare static variables in the class scope. These, as you'd expect, are accessible to all instances and class (aka static) methods.
See: http://www.otierney.net/objective-c.html#class
Can you tell me how long a variable lives in a program (in Java). i.e. variables declared inside methods, variables used in parameters, STATIC variables, variables used to return from a method, etc.
Thanks.
References declared inside methods go out of scope when the method exits.
Parameters passed to methods won't be eligible for GC until there are no more references to them or they go out of scope.
Static references are associated with a class and live as long as the class is loaded.
Returned references won't be eligible for GC until there are no more references to them or they go out of scope.
Objects in Java are guaranteed to live as long as they are referenceable through a transitive closure of the root set. This is a conservative approximation of the application's live objects¹.
Edit: Is this question concerning object lifetime, or variable scope? We have some mixed terminology going on.
¹ A live object is an object that will be referenced again before the application terminates.
Life Spam hierarchy
Class Variable > Instance Variable > Method variable > Local Variable
Class variable has the longest life spam and local variable has least, in-fact the local variable are only accessible within the block we define them in.