Questions tagged [class-variables]

A class variable is a variable shared by all instance of that class. In some languages, it is equivalent to declaring it with a `static` modifier, but that is not always that simple (there are language for which the two are not synonymous).

A class variable is a variable shared by all instance of that class. In some languages, it is equivalent to declaring it with a static modifier, but that is not always that simple (there are language for which the two are not synonymous).

472 questions
2514
votes
27 answers

Class (static) variables and methods

How do I create class (i.e. static) variables or methods in Python?
Andrew Walker
  • 40,984
  • 8
  • 62
  • 84
229
votes
6 answers

How can I access "static" class variables within methods?

If I have the following code: class Foo(object): bar = 1 def bah(self): print(bar) f = Foo() f.bah() It complains NameError: global name 'bar' is not defined How can I access class/static variable bar within method…
Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
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
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
187
votes
6 answers

Is final ill-defined?

First, a puzzle: What does the following code print? public class RecursiveStatic { public static void main(String[] args) { System.out.println(scale(5)); } private static final long X = scale(10); private static long…
Little Helper
  • 1,870
  • 3
  • 12
  • 20
160
votes
9 answers

What is the difference between up-casting and down-casting with respect to class variable

What is the difference between up-casting and down-casting with respect to class variable? For example in the following program class Animal contains only one method but Dog class contains two methods, then how we cast the Dog variable…
Dhivakar
  • 2,081
  • 5
  • 15
  • 18
127
votes
4 answers

Create module variables in Ruby

Is there any way to create a variable in a module in Ruby that would behave similar to a class variable? What I mean by this is that it would be able to be accessed without initializing an instance of the module, but it can be changed (unlike…
Mark Szymanski
  • 56,590
  • 24
  • 70
  • 87
104
votes
5 answers

How do I set and access attributes of a class?

Suppose I have this code: class Example(object): def the_example(self): itsProblem = "problem" theExample = Example() print(theExample.itsProblem) When I try it, I get an error that says: Traceback (most recent call last): File…
Adam
  • 1,061
  • 2
  • 8
  • 6
96
votes
3 answers

Difference between class variables and class instance variables?

Can anyone tell me about the difference between class variables and class instance variables?
cmd
  • 1,013
  • 1
  • 8
  • 8
54
votes
2 answers

Can I access a class variable from an instance?

I have this class: class ReallyLongClassName: static_var = 5 def instance_method(self): ReallyLongClassName.static_var += 1 Is there some way to access the static variable using the self variable? I'd rather do something like…
Jacklynn
  • 1,503
  • 1
  • 13
  • 23
52
votes
5 answers

Constants or class variables in ruby?

I've been programming in Ruby for a few months now, and I'm wondering when it is appropriate to use constants over class variables and vice versa. (I'm working in Rails, thinking about constants in models). class Category TYPES = %w(listing event…
Ben Crouse
  • 8,290
  • 5
  • 35
  • 50
41
votes
2 answers

python subclass access to class variable of parent

I was surprised to to learn that a class variable of a subclass can't access a class variable of the parent without specifically indicating the class name of the parent: >>> class A(object): ... x = 0 ... >>> class B(A): ... y = x+1 ...…
new name
  • 15,861
  • 19
  • 68
  • 114
39
votes
4 answers

why are java constants declared static?

Why are Java constants declared static? class Foo { static final int FII = 2 ; } In this example I understand the use of final, But why does it have to be static? Why should it be a class variable and not an instance variable?
Vaibhav
  • 435
  • 1
  • 7
  • 11
38
votes
7 answers

how to create class variable dynamically in python

I need to make a bunch of class variables and I would like to do it by looping through a list like that: vars=('tx','ty','tz') #plus plenty more class Foo(): for v in vars: setattr(no_idea_what_should_go_here,v,0) is it possible? I…
pawel
  • 1,031
  • 2
  • 9
  • 16
30
votes
4 answers

Why my program doesn't show compile time error when final class variable is not initialized?

For following code: public class StaticFinal { private final static int i ; public StaticFinal() {} } I get compile time error: StaticFinal.java:7: variable i might not have been initialized {} ^ 1 error Which is in…
Vishal K
  • 12,976
  • 2
  • 27
  • 38
1
2 3
31 32