# Static and class variable?

**URL:** <https://www.ruby-forum.com/t/static-and-class-variable/141883>\
**Category:** Rails\
**Created:** [July 1, 2008, 11:53am UTC](https://www.ruby-forum.com/t/static-and-class-variable/141883 "2008-07-01T11:53:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratnavel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6f9a4e/32.png) [@ratnavel](https://www.ruby-forum.com/u/ratnavel)\
**Post date:** [July 1, 2008, 11:53am UTC](https://www.ruby-forum.com/t/static-and-class-variable/141883/1 "2008-07-01T11:53:06Z")

</div>

In rails, What is the difference between static and class variable? Is  
it the same or different?

Thanks,  
Ratnavel

---

<div class="post-metadata">

**Author:** ![ratnavel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6f9a4e/32.png) [@ratnavel](https://www.ruby-forum.com/u/ratnavel)\
**Post date:** [July 1, 2008, 1:17pm UTC](https://www.ruby-forum.com/t/static-and-class-variable/141883/2 "2008-07-01T13:17:35Z")

</div>

It’s different

class variables are variables of the class  
Their value is shared by all objects

Class Something  
@@class\_variable = 0

def do\_something  
@@class\_variable +=1  
end  
end

foo = Something.new  
foo.do\_something  
bar = Something.new  
bar.do\_something

After that code @@class\_variable will be 2  
It behaves like a static class variable in C++

There are no static variables on a function level like in C++ as far  
as I know  
You can use normal class attributes or class variables instead  
(depending if you want them per object or not)

Here is some longer discussion on the topic:  
[http://groups.google.com/group/ruby-talk-google/browse\_thread/thread/30c79cd4b9fb5c8](http://groups.google.com/group/ruby-talk-google/browse_thread/thread/30c79cd4b9fb5c8)
