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
25
votes
3 answers

Why does Scala language require you initialize a instance variable instead of relying on a default value?

Scala language requires you initialize your instance variable before using it. However, Scala does not provide a default value for your variable. Instead, you have to set up its value manually by using the wildcard underscore, which acts like a…
Arthur Ronald
  • 33,349
  • 20
  • 110
  • 136
23
votes
3 answers

Private ivar in @interface or @implementation

Is there any reason to declare a private ivar in @interface instead of @implementation? I see code like this all over the internet (including documentation provided by Apple): Foo.h @interface Foo : NSObject { @private id…
Yevgeniy
  • 2,614
  • 1
  • 19
  • 26
23
votes
6 answers

Static vs Instance Variables: Difference?

What is the difference between a static and instance variable. The following sentence is what I cant get: In certain cases, only one copy of a particular variable should be shared by all objects of a class- here a static variable is used. A…
user2971033
23
votes
10 answers

Java instance variables vs. local variables

I'm in my first programming class in high school. We're doing our end of the first semester project. This project only involves one class, but many methods. My question is about best practice with instance variables and local variables. It seems…
user218251
  • 231
  • 1
  • 2
  • 3
22
votes
4 answers

Properties for Class and Its Subclasses Only

Is it possible to define properties that are only available to the class they are defined in, and that class's subclasses? Stated another way, is there a way to define protected properties?
Jay Haase
  • 1,979
  • 1
  • 16
  • 36
22
votes
2 answers

Local variables set to nil? (Objective-C)

I'm reading a book on Objective-C and the author said that if local variables aren't assigned a value they will be set to nil, but static variables will be set to zero. So, I set up int a and didn't assign it a value. Then NSLog(@"%i", a) to display…
stumped
  • 3,235
  • 7
  • 43
  • 76
21
votes
2 answers

Instance Variables in Javascript Classes

I mostly code in PHP and Java, but I will occasionally work on the front end of a project and use JavaScript. I usually create objects differently than below, but I came across this and it caught my interest being that the syntax is similar to what…
aCarella
  • 2,369
  • 11
  • 52
  • 85
21
votes
1 answer

'Use of self in method call before super.init initializes self', can't init properties through a method call

I'm curious is there is anyway to call a method inside your init method that sets instance properties of the class. Essentially I just have a class that sub-classes UIView, adds some subviews in init, and some of those subviews are instance…
Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
21
votes
3 answers

How do instance variables in rspec work?

Here is a bit of code from M Hartl's Ruby on Rails Tutorial. Can anyone explain why an instance variable (@user) is necessary and why not use a local variable. Also, since instance variables are supposed to be the variables in the instance of a…
20
votes
3 answers

Relying on default field initialisation - is bad programming style?

I was given a link to the official oracle documentation: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html where it is said: Default Values It's not always necessary to assign a value when a field is declared. Fields that…
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
20
votes
2 answers

how do I test that an instance variable is set in my my mailer with rspec?

How do I test that a certain instance variable is set in my my mailer with rspec? assigns is coming back undefined.. require File.dirname(__FILE__) + '/../../spec_helper' describe UserMailer do it "should send the member user password to a User"…
pixelearth
  • 13,674
  • 10
  • 62
  • 110
20
votes
3 answers

Inherit class-level instance variables in Ruby?

I want a child class to inherit a class-level instance variable from its parent, but I can't seem to figure it out. Basically I'm looking for functionality like this: class Alpha class_instance_inheritable_accessor :foo # @foo = [1, 2,…
wmjbyatt
  • 686
  • 5
  • 15
19
votes
2 answers

Python: abstract instance variable?

Is there a way to declare an abstract instance variable for a class in python? For example, we have an abstract base class, Bird, with an abstract method fly implemented using the abc package, and the abstract instance variable feathers (what I'm…
18
votes
9 answers

What is the correct (or best) way to subclass the Python set class, adding a new instance variable?

I'm implementing an object that is almost identical to a set, but requires an extra instance variable, so I am subclassing the built-in set object. What is the best way to make sure that the value of this variable is copied when one of my objects is…
rog
  • 6,252
  • 4
  • 31
  • 25
18
votes
9 answers

Ruby class instance variables and inheritance

I have a Ruby class called LibraryItem. I want to associate with every instance of this class an array of attributes. This array is long and looks something like ['title', 'authors', 'location', ...] Note that these attributes are not really…
rlandster
  • 7,294
  • 14
  • 58
  • 96