---
title: "java support instance variables of java enum constants"  
description: "java support instance variables of java enum constants"  
author: "Anonymous User"  
published: 2014-10-16  
updated: 2014-10-16  
canonical: https://www.mindstick.com/forum/2387/java-support-instance-variables-of-java-enum-constants  
category: "java"  
tags: ["java", "oops"]  
reading_time: 2 minutes  

---

# java support instance variables of java enum constants

I know that in Java an enum [constant](https://www.mindstick.com/forum/34714/difference-between-statics-vs-constant-in-c-sharp) is implicitly static [final variable](https://www.mindstick.com/interview/2386/what-is-blank-final-variable-in-java). But the enum class can have an [instance variable](https://www.mindstick.com/forum/23344/multidimensional-array-of-nsinteger-as-instance-variable) say size. So each enum constant will have a copy of 'size'. What is the equivalent Java code for this? I mean it "seems" the static enum constant is using a non-static instance variable which is not possible normally?

```
enum Members{        A(1),B(2),C(3);  // I have 3 enum constants here         private int size;        Members (int size) {            //System.out.println("Initializing var with size = "+size);        }    }
```

Equivalent code I know so far:\

```
public final class Member extends Enums<Members> {        public static final Members A;        // ...        // What happened to size? How do A,B,C get a copy of size?    }
```

**Edit :** To restate my [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time)- I am interested in behind the scene [implementation](https://www.mindstick.com/forum/33764/how-to-implementation-of-class-in-c-sharp) by [compiler](https://www.mindstick.com/articles/27/just-in-time-compiler). I [already](https://yourviews.mindstick.com/view/88453/the-hidden-ways-ai-is-already-controlling-your-daily-life) know how to use enums. I [am looking](https://answers.mindstick.com/qa/115463/i-am-looking-for-to-joining-the-mindstick-internship-program-how-to-apply) for what the compiler does? (so for example, if I simply write A, the compiler translates it to "public static final Member A". I want to know how compiler gives a copy of size to each A,B,C.\

## Replies

### Reply by Anonymous User

I mean it "seems" the static enum constant is using a non-static [instance](https://www.mindstick.com/forum/155658/testing-connection-to-cloud-sql-instance-with-a-mysql-client-from-vm-instance) [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) which is not possible normally?\
It is absolutely possible: each static variable in the enum is an object in its own right, complete with its instance variables. The instance is static in the enum, but it does not make the context of the instance itself a static context.

```
public final class Member extends java.lang.Enums<Members> {    public static final Members A = new Member(1) {        public String toString() { return "A:"+size; }    };    public static final Members B = new Member(2) {        public String toString() { return "B:"+size; }    };    public static final Members C = new Member(3) {        public String toString() { return "C:"+size; }    };    private final int size;    protected Member(int size) { this.size = size; }}
```


---

Original Source: https://www.mindstick.com/forum/2387/java-support-instance-variables-of-java-enum-constants

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
