In object-oriented programming with classes, an instance variable is a variable defined in a class (i.e. a member variable), for which each object of the class has a separate copy.
80
votes
6answers
20k views
Properties and Instance Variables in Objective-C 2.0
Do properties in Objective-C 2.0 require a corresponding instance variable to be declared? For example, I'm used to doing something like this:
MyObject.h
@interface MyObject : NSObject {
NSString ...
67
votes
1answer
8k views
Directly accessing an instance variable vs. Using an accessor method
Can anyone explain the difference between accessing an instance attribute via self.attribute and by @attribute?
45
votes
3answers
9k views
Are ints always initialized to 0?
Is it safe to count on ints always being initialized to 0 in Objective-C?
More specifically, when an object with int ivars has been newly instantiated, is it safe to assume that its ivars have value ...
40
votes
6answers
10k views
Private members in CoffeeScript?
does somebody know how to make private, non-static members in CoffeeScript? Currently I'm doing this, which just uses a public variable starting with an underscore to clarify that it shouldn't be used ...
37
votes
5answers
13k views
Properties and Instance Variables in Objective-C
I'm rather confused about properties and instance variables in Objective-C.
I'm about half-way through Aaron Hillegass's "Cocoa Programming for Mac OS X" and everything is logical. You would declare ...
36
votes
3answers
6k views
Instance variable: self vs @
I saw a code
class Person
def initialize(age)
@age = age
end
def age
@age
end
def age_difference_with(other_person)
(self.age - other_person.age).abs
end
protected :age
end
...
36
votes
3answers
5k views
What is the difference between ivars and properties in Objective-C
I'm pretty sure this question hasn't been formulated in this way before, but if it has please accept my apologies. Basically, I would like a clear definitive explanation regarding the semantic ...
35
votes
8answers
34k views
Python - Get Instance Variables
Is there a built-in method in Python to get an array of all a class' instance variables? For example, if I have this code:
class hi:
def __init__(self):
self.ii = "foo"
self.kk = "bar"
Is ...
33
votes
4answers
24k views
How can I initialize a module's instance variables in Ruby?
I have some modules where I would like to use instance variables in. I'm currently initializing them like this:
module MyModule
def self.method_a(param)
@var ||= 0
# other logic goes here
...
32
votes
4answers
11k views
iOS: must every iVar really be property?
I see it recommended all over the place that when coding for iOS, properties should be used for accessing instance variables because of the benefits this lends to memory management, among other ...
27
votes
5answers
16k views
When do Ruby instance variables get set?
class Hello
@hello = "hello"
def display
puts @hello
end
end
h = Hello.new
h.display
I created the class above. It doesn't print anything out. I thought the instance variable @hello ...
26
votes
6answers
25k views
How to make a real private instance variable?
I want to make an instance variable that can't be accessed from outside. Is something like that possible in objective-c? I remember Apple has private variables and stuff like that, but if people know ...
26
votes
3answers
11k views
What does @@variable mean in Ruby?
What are Ruby variables preceded with double at signs (@@)? My understanding of a variable preceded with an at sign is that it is an instance variable, like this in PHP:
PHP version
class Person {
...
20
votes
7answers
8k views
Object-Oriented Perl constructor syntax
I'm a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot.
package Foo;
#In Perl, the constructor is just a subroutine called new.
sub new {
#I ...
20
votes
8answers
10k views
Instance variable initialization in java
I have doubt in instance variable initialization in java.I have seen different developers initializing varibles differently.In the below sample codes what is better way.Do we have any advantage of ...