# Is final variable changing its value throughout time?

I’m currently teaching myself Java from a textbook, and found a piece of code that I don’t understand – why does the final variable `id` change it’s value?

```import static net.mindview.util.Print.*;
class Shared {
private int refcount = 0;
private static int counter = 0;
private final int id = counter++;
public Shared() {
print("Creating " + this);
}
public void addRef() { refcount++; }
protected void dispose() {
if(--refcount == 0)
print("Disposing " + this);
}
protected void finalize() {
if(refcount != 0)
print("Error: object is not properly cleaned-up!");
}
public String toString() { return "Shared " + id; }
}
class Composing {
private Shared shared;
private static int counter = 0;
private final int id = counter++;
public Composing(Shared shared) {
print("Creating " + this);
this.shared = shared;
this.shared.addRef();
}
protected void dispose() {
print("disposing " + this);
shared.dispose();
}
public String toString() { return "Composing " + id; }
}
public class E13_VerifiedRefCounting {
public static void main(String[] args) {
Shared shared = new Shared();
Composing[] composing = { new Composing(shared),
new Composing(shared), new Composing(shared),
new Composing(shared), new Composing(shared) };
for(Composing c : composing)
c.dispose();
```

## Answer

This:

```    private static int counter = 0;
private final int id = counter++;
public Shared() {
print("Creating " + this);
}
```

is the same as this:

```    private static int counter = 0;
private final int id;
public Shared() {
id = counter++;
print("Creating " + this);
}
```

That is, the `id` is assigned every time the constructor executes, with the side effect that `counter` is incremented.

`id` is an instance variable, so each instance of `shared` gets its own `id` value.
{"ft_lang_label":"__label__en","ft_lang_prob":0.65189254,"math_prob":0.91489273,"size":1714,"snap":"2021-04-2021-17","text_gpt3_token_len":410,"char_repetition_ratio":0.17192982,"word_repetition_ratio":0.22846442,"special_character_ratio":0.28296384,"punctuation_ratio":0.18367347,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96175325,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-18T04:52:26Z\",\"WARC-Record-ID\":\"<urn:uuid:460f8eb6-6ca7-4873-b8ec-9dc1775284b7>\",\"Content-Length\":\"43980\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4315cb2a-455c-42f5-88b6-fe6c21013be1>\",\"WARC-Concurrent-To\":\"<urn:uuid:06421ef0-e565-4c07-a883-76d3eafa8908>\",\"WARC-IP-Address\":\"104.21.82.24\",\"WARC-Target-URI\":\"https://www.tutorialguruji.com/java/is-final-variable-changing-its-value-throughout-time/\",\"WARC-Payload-Digest\":\"sha1:Y63B5RKGM4ZJB2CUNFVDGJMPGA3IQ3CT\",\"WARC-Block-Digest\":\"sha1:YT6HH2YQJARWVASHEB3L2ZGMON3V52R6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038468066.58_warc_CC-MAIN-20210418043500-20210418073500-00354.warc.gz\"}"}