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
1250
votes
8 answers

How do servlets work? Instantiation, sessions, shared variables and multithreading

Suppose, I have a webserver which holds numerous servlets. For information passing among those servlets I am setting session and instance variables. Now, if 2 or more users send request to this server then what happens to the session variables? Will…
Ku Jon
  • 12,503
  • 3
  • 16
  • 4
253
votes
15 answers

Should I instantiate instance variables on declaration or in the constructor?

Is there any advantage for either approach? Example 1: class A { B b = new B(); } Example 2: class A { B b; A() { b = new B(); } }
DonX
  • 16,093
  • 21
  • 75
  • 120
217
votes
8 answers

Ruby class instance variable vs. class variable

I read When do Ruby instance variables get set? but I'm of two minds when to use class instance variables. Class variables are shared by all objects of a class, Instance variables belong to one object. There's not much room left to use class…
Elmor
  • 4,775
  • 6
  • 38
  • 70
195
votes
7 answers

Instance variable: self vs @

Here is some 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 What I want to know is the difference…
sarunw
  • 8,036
  • 11
  • 48
  • 84
191
votes
5 answers

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 { public $name; public function…
Andrew
  • 227,796
  • 193
  • 515
  • 708
154
votes
12 answers

How to get instance variables in Python?

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 there a way for me to do this: >>>…
Chris Bunch
  • 87,773
  • 37
  • 126
  • 127
153
votes
20 answers

Ruby convert Object to Hash

Let's say I have a Gift object with @name = "book" & @price = 15.95. What's the best way to convert that to the Hash {name: "book", price: 15.95} in Ruby, not Rails (although feel free to give the Rails answer too)?
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
145
votes
3 answers

Explain __dict__ attribute

I am really confused about the __dict__ attribute. I have searched a lot but still I am not sure about the output. Could someone explain the use of this attribute from zero, in cases when it is used in a object, a class, or a function?
Zakos
  • 2,448
  • 4
  • 16
  • 20
103
votes
4 answers

Ruby Metaprogramming: dynamic instance variable names

Let's say I have the following hash: { :foo => 'bar', :baz => 'qux' } How could I dynamically set the keys and values to become instance variables in an object... class Example def initialize( hash ) ... magic happens here... end end ...…
Andrew
  • 42,517
  • 51
  • 181
  • 281
103
votes
6 answers

Do declared properties require a corresponding instance variable?

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 *name; } @property (nonatomic, retain) NSString…
indragie
  • 18,002
  • 16
  • 95
  • 164
99
votes
3 answers

Access instance variable from outside the class

If an instance variable belongs to a class, can I access the instance variable (e.g. @hello) directly using the class instance? class Hello def method1 @hello = "pavan" end end h = Hello.new puts h.method1
Pawan
  • 31,545
  • 102
  • 256
  • 434
95
votes
6 answers

How can I use an attribute of the instance as a default argument for a method?

I want to pass a default argument to an instance method using the value of an attribute of the instance: class C: def __init__(self, format): self.format = format def process(self, formatting=self.format): …
Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197
87
votes
1 answer

Ruby Rspec : Testing instance variables without adding an accessor to source

I'm trying to test the following method: def unprocess_move(board, move) if move[0].instance_of?(Array) multi_move = @multi_move.pop(2).reverse multi_move.each do |single_move| unapply_move(board, single_move) end else …
steve_gallagher
  • 3,778
  • 8
  • 33
  • 51
86
votes
5 answers

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 end end I also could call a init method to…
Geo
  • 93,257
  • 117
  • 344
  • 520
84
votes
11 answers

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 outside of the class: class Thing extends…
thejh
  • 44,854
  • 16
  • 96
  • 107
1
2 3
99 100