# Class with static fields

**URL:** <https://racket.discourse.group/t/class-with-static-fields/2530>\
**Category:** General\
**Tags:** object-oriented\
**Created:** [November 23, 2023, 6:49pm UTC](https://racket.discourse.group/t/class-with-static-fields/2530 "2023-11-23T18:49:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ceving](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@ceving](https://racket.discourse.group/u/ceving)\
**Post date:** [November 23, 2023, 6:49pm UTC](https://racket.discourse.group/t/class-with-static-fields/2530/1 "2023-11-23T18:49:36Z")

</div>

What is Rackets canonical way to define fields called in other languages "[static](https://www.baeldung.com/java-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:

```scheme
#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)

```

---

<div class="post-metadata">

**Author:** ![joeld](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/joeld/32/38_2.png) [@joeld](https://racket.discourse.group/u/joeld)\
**Post date:** [November 23, 2023, 7:17pm UTC](https://racket.discourse.group/t/class-with-static-fields/2530/2 "2023-11-23T19:17:15Z")

</div>

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:

```scheme
(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](https://beautifulracket.com/explainer/parameters.html) 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.

---

<div class="post-metadata">

**Author:** ![ceving](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@ceving](https://racket.discourse.group/u/ceving)\
**Post date:** [November 23, 2023, 7:27pm UTC](https://racket.discourse.group/t/class-with-static-fields/2530/3 "2023-11-23T19:27:02Z")

</div>

> [@joeld](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![EmEf](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@EmEf](https://racket.discourse.group/u/EmEf)\
**Post date:** [November 24, 2023, 12:59am UTC](https://racket.discourse.group/t/class-with-static-fields/2530/4 "2023-11-24T00:59:14Z")

</div>

Will this make you happier?

```scheme
#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 

```
