Class with static fields

What is Rackets canonical way to define fields called in other languages "static", which means the value is shared among all instances of the class. I can use a closure, but I can not access the value without an object:

#lang racket

(define parent%
  (let ((i 0))
    (class object%
      (super-new)
      (define/public (inc)
        (set! i (+ i 1)))
      (define/public (get)
        i))))

(define child%
  (class parent%
    (super-new)
    (inherit inc)))

(define p (new parent%))
(define c1 (new child%))
(define c2 (new child%))

(send p inc)
(send c1 inc)
(send c2 inc)
(send p get)

If you want to be able to access the variable without going through an object, you could create additional function(s) for doing so inside the closure:

(define-values (parent% peek)
  (let ((i 0))
    (values
     (class object%
       (super-new)
       (define/public (inc)
         (set! i (+ i 1)))
       (define/public (get)
         i))
     (lambda () i))))

(define child%
  (class parent%
    (super-new)
    (inherit inc)))

(define p (new parent%))
(define c1 (new child%))
(define c2 (new child%))

(send p inc)
(send c1 inc)
(send c2 inc)
(peek)

Another approach would be module-level variable or parameter that is defined in the same module as the class. I admit it’s not super OOPy but I’m having trouble imagining why anything more elaborate would be desirable.

If the static field belongs to the class, you need only one value to access it. Your example requires two values: 1st the class and 2nd the closure.

Will this make you happier?

#lang racket

(module static-var racket
  (provide shared%)

  (provide static)
  
  [define static 0]

  (define shared%
    (class object%
      (super-new)
      (define/public (set x) (set! static x) static))))

(require 'static-var)

(send (new shared%) set 22)

static