---
title: "How to set value for constant in java after declaration?"  
description: "How to set value for constant in java after declaration?"  
author: "Lillian Martin"  
published: 2014-11-13  
updated: 2014-11-13  
canonical: https://www.mindstick.com/forum/12572/how-to-set-value-for-constant-in-java-after-declaration  
category: "java"  
tags: ["variable"]  
reading_time: 2 minutes  

---

# How to set value for constant in java after declaration?

For simplicity I will use a basic example.

If I am using a record ([struct](https://www.mindstick.com/blog/83/struct-in-c-sharp-dot-net)) in my Java [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) like so:

```
public class Store{  class Product{    final int item1;    final int item2;    final int item3;  }
```

and I create a constructor for my [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) that will take [values](https://www.mindstick.com/forum/327/sum-textbox-values) to represent the [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) of each item:

```
  public Store(int[] elements) {     Product x = new Product();     x.item1 = elements[0];     x.item2 = elements[1];     x.item3 = elements[2];  }}
```

**The [compiler](https://www.mindstick.com/articles/27/just-in-time-compiler) gives me two [errors](https://answers.mindstick.com/qa/116170/fresh-fir-against-gandhis-in-national-herald-case-cover-up-for-ed-s-own-errors):**

"The [blank final](https://www.mindstick.com/interview/2386/what-is-blank-final-variable-in-java) [field](https://www.mindstick.com/forum/160867/does-mindstick-provide-training-for-field-marketing) item1 may not have been initialized

"The final field cannot be assigned"

I understand that we can not re-assign values to [constants](https://www.mindstick.com/articles/1813/objective-c-constants), but is there a way to assign values to uninitialized constants?

## Replies

### Reply by Mark M

The only way is to assign such values in the constructor, so you would need to add a constructor to your structure class:

```
class Product{    Product(double item1, double item2, double item3) {        this.item1 =item1;        this.item2 =item2;        this.item3 = item3;    }    final double item1;    final double item2;    final double item3;}
```

**And then use it in the rest of your code:**

```
public Store(int[] elements) {    Product x = new Product(elements[0],
elements[1], elements[2]);} 
```


---

Original Source: https://www.mindstick.com/forum/12572/how-to-set-value-for-constant-in-java-after-declaration

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
