1

If you look around you'll find some comparisons of class variables to class instance variables, and the best I ever hear of class variables are "that might be what you want", but I've never heard anybody say that is what they want, or when the situation of sharing a variable across an entire inheritance tree might be more useful.

So in practice, when is a class variable a better choice than a class instance variable?

Community
  • 1
  • 1
ian
  • 12,003
  • 9
  • 51
  • 107
  • Are they the same as static fields in Java? They are theoretically useful but I've never used them (except static final ones, i.e. constants) – John Dvorak Nov 02 '12 at 08:53
  • If all objects of some class must share same state(stupid example: count of instances of class) then you have candidate for class variable(static in other languages) – Igor Nov 02 '12 at 08:57
  • @JanDvorak Effectively, yes - but they are implemented differently. –  Nov 02 '12 at 09:06
  • @icrew yes, I understand that, but the question is about class variables over class _instance_ variables. Both can do that job but I've only found the later to be any practical use. – ian Nov 02 '12 at 09:13

2 Answers2

1

Another example(you want unique name for every object)

class Foobar
    @@uniqueId = 0        
    def initialize
       @@uniqueId += 1
    end        
    def unique_name
       "Foobar_" + @@uniqueId.to_s
    end
end
a = Foobar.new
puts a.unique_name

a = Foobar.new
puts a.unique_name

This will output

Foobar_1

Foobar_2

Edit: Singleton pattern is also good example for static variables link

Igor
  • 1,835
  • 17
  • 15
  • This may be the only example on the internet of a good use of a class variable :) Again though, this only has an advantage over a class instance variable if this behaviour is also desired for subclasses too, and you could do this with a class instance variable (but not as easily, granted). – ian Nov 02 '12 at 09:15
  • Sometimes you want to be something global for all instances of class and it subclasses. In above example(again stupid) maybe you want to set something like @@unique_name_suffix for all, so unique_name suffix will be "Foobar_" + @@unqiueId.to_s + @@unique_name_suffix When you go deeper in programming complex system you will figure out by yourself when you need static variable and when you need instance variable. – Igor Nov 02 '12 at 09:25
  • I already program complex systems... ;) it doesn't mean a language construct shouldn't be explored by asking the simple question. As to the link, it shows that Rails uses class instance variables instead. A real practical example. – ian Nov 02 '12 at 09:54
  • Sorry for that remark with complex systems. I regret my sentence as soon as I look at your profile :). My bad assumption. I just wanted to say that sometimes is easier to use static variable if something wanted to be unique across the all objects. At the end, it is all on developer to decide should he/she use static variables or not. – Igor Nov 02 '12 at 10:01
0

The easiest example would be that you use class variable to store some overview states of that class or shared states of all instances.

class Foobar
  @@total = 0

  def initialize
    @@total += 1
  end

  class << self
    def all
      @@total
    end
  end
end

5.times { Foobar.new }
Foobar.all #=> 5

So here @@total will show you how many instances have been created as a sum.

Rails' ActiveRecord#FinderMethods#all simply returns the array's size. But if all your instances of a class are not wrapped in a array, using class variable to return "all" is also a solution.

Jing Li
  • 14,547
  • 7
  • 57
  • 69
  • If I wanted to keep track of the number of all instances _and_ subclass instances, I suppose this would be useful. Otherwise I don't see the advantage over a class instance variable. – ian Nov 02 '12 at 09:12