Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static final BigDecimal in Java class

Tags:

java

I'm a bit unsure of how I implemented something, and am hoping for some feedback.

I have a class, Metric, and it needs to multiply some numbers by a given percentage before returning them. They are returning BigDecimal, so I created class variable BigDecimal to store that percentage, then multiply them together on the return.

public class Metric extends Model {
    private static final BigDecimal percentage = new BigDecimal("1.2");

    public BigDecimal getMetric() {
        return new BigDecimal(getValue()).multiply(percentage);
    }
}

Is there any issue with a static final and declaring it right away with new? Also, I tried to research if BigDecimal is thread safe, and I couldn't find a for sure answer. Feedback on that would be appreciated.

like image 530
Ross Hettel Avatar asked Oct 17 '25 13:10

Ross Hettel


1 Answers

BigDecimal is immutable and thus thread-safe. Also, there is no problem with a static final value like that -- in fact, it's recommended.

like image 111
Thorn G Avatar answered Oct 20 '25 04:10

Thorn G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!