W3cubDocs

/Crystal

Class variables

Class variables are associated to classes instead of instances. They are prefixed with two "at" signs (@@). For example:

class Counter
  @@instances = 0

  def initialize
    @@instances += 1
  end

  def self.instances
    @@instances
  end
end

Counter.instances # => 0
Counter.new
Counter.new
Counter.new
Counter.instances # => 3

Class variables can be read and written from class methods or instance methods.

Their type is inferred using the global type inference algorithm.

Class variables are inherited by subclasses with this meaning: their type is the same, but each class has a different runtime value. For example:

class Parent
  @@numbers = [] of Int32

  def self.numbers
    @@numbers
  end
end

class Child < Parent
end

Parent.numbers # => []
Child.numbers  # => []

Parent.numbers << 1
Parent.numbers # => [1]
Child.numbers  # => []

Class variables can also be associated to modules and structs. Like above, they are inherited by including/subclassing types.

To the extent possible under law, the persons who contributed to this workhave waived
all copyright and related or neighboring rights to this workby associating CC0 with it.
https://crystal-lang.org/docs/syntax_and_semantics/class_variables.html