Questions tagged [instance-variables]

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.

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.

1506 questions
82
votes
2 answers

What is the difference between ivars and properties in Objective-C

What is the semantic difference between these 3 ways of using ivars and properties in Objective-C? 1. @class MyOtherObject; @interface MyObject { } @property (nonatomic, retain) MyOtherObject *otherObj; 2. #import "MyOtherObject.h" @interface…
ennuikiller
  • 46,381
  • 14
  • 112
  • 137
80
votes
2 answers

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?
pistacchio
  • 56,889
  • 107
  • 278
  • 420
79
votes
4 answers

How to deal with Pylint's "too-many-instance-attributes" message?

I have just tried to lint some code with Pylint, and the last remaining error is R0902: too-many-instance-attributes (8/7) I understand the rationale behind limiting the number of instance attributes, but seven seems a bit low. I also realise that…
76
votes
3 answers

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 0?
Felixyz
  • 19,053
  • 14
  • 65
  • 60
72
votes
2 answers

Using Instance Variables in Class Methods - Ruby

I have a class something like below, and I used instance variables (array) to avoid using lots of method parameters. It works as I expected but is that a good practice? Actually I wouldn't expect that worked, but I guess class methods are not…
tackleberry
  • 1,003
  • 2
  • 12
  • 16
72
votes
3 answers

Passing variables, creating instances, self, The mechanics and usage of classes: need explanation

I just rewrote a working program into functions in a class and everything messed up. First, in the __init__ section of the class I declared a bunch of variables with self.variable=something. Should I be able to access/modify these variables in every…
Baf
  • 2,041
  • 3
  • 16
  • 10
67
votes
6 answers

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 was set during the class declaration. But when I…
pez_dispenser
  • 4,394
  • 7
  • 37
  • 47
67
votes
3 answers

Should all member variables be initialized in __init__

Maybe this is more of a style question than a technical one but I have a class with several member variables and I want to have it work so that some of the member variables are initialized when the user first creates an instance of the class (i.e.…
user1893354
  • 5,778
  • 12
  • 46
  • 83
57
votes
4 answers

Must every ivar be a property?

I see it recommended all over the place when coding for iOS that properties should be used for accessing instance variables because of the benefits this lends to memory management, among other things. This advice doesn't sit terribly well with me. I…
Diego
  • 595
  • 1
  • 5
  • 6
56
votes
5 answers

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 a class something like this: @class…
Steve Harrison
  • 121,227
  • 16
  • 87
  • 72
51
votes
7 answers

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 about them, they can use them. Apple calls that…
HelloMoon
45
votes
7 answers

How to make instance variables private in Ruby?

Is there any way to make instance variables "private"(C++ or Java definition) in ruby? In other words I want following code to result in an error. class Base def initialize() @x = 10 end end class Derived < Base def x @x = 20 …
prasadvk
  • 1,568
  • 2
  • 13
  • 13
43
votes
2 answers

What is an instance variable in Java?

My assignment is to make a program with an instance variable, a string, that should be input by the user. But I don't even know what an instance variable is. What is an instance variable? How do I create one? What does it do?
aqua
  • 719
  • 4
  • 11
  • 17
41
votes
6 answers

Rails: access controller instance variable in CoffeeScript or JavaScript asset file

In Rails 3.1 it is not possible to access controller instance variables in an asset js.erb or coffee.erb file using syntax such as <%= @foo %>, where @foo is set in the controller. So then the question is what are the best ways for passing…
Safa Alai
  • 1,223
  • 1
  • 15
  • 27
39
votes
4 answers

Should internal class methods return values or just modify instance variables?

I am creating a query builder class that will help in constructing a query for mongodb from URL params. I have never done much object oriented programming, or designed classes for consumption by people other than myself, besides using basic…
dm03514
  • 54,664
  • 18
  • 108
  • 145