static value need to get it from constant file in java?

Posted on 2016-11-08
Last Modified: 2016-11-13
Hi Experts,
I have a constant value used at one place
do we need to keep it in constants file and need to get it from constants file?
If it is used in multiple place then i will keep it in constants file bcoz if any change happened i need to modify every place
If it is used in one place why we need to keep it in constant file we can use directly am i correct?
Question by:srikotesh
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 27

Accepted Solution

dpearson earned 250 total points
ID: 41879950
If it's a static value you can put it anywhere and still reference it in other places:

public class MyClass {
   public final static double kTaxRate= 8.3 / 100.0 ;

   public double getTaxRate() {
      return kTaxRate ;

public class MyOtherClass {
   public double computeTax(double cost) {
      return  cost * MyClass.kTaxRate ;

Open in new window

So it doesn't need to be in a special "constants file" but if you should declare a constant variable somewhere.

What you shouldn't do is:

public class MyClass {
   public double getTaxRate() {
      return 8.3 / 100.0 ;

public class MyOtherClass {
   public double computeTax(double cost) {
      return  cost * 8.3 / 100.0 ;

Open in new window

Hope that helps,

LVL 86

Assisted Solution

CEHJ earned 250 total points
ID: 41880299
If it is used in one place why we need to keep it in constant file we can use directly am i correct?
You don't really need to, though you might say the following:
a. what if it's a general attribute and doesn't belong to a specific domain object. Let's say - the speed of light?
b. what if it later becomes necessary to address that value from various places. It would be better to refer to it as Constants.SPEED_OF_LIGHT than Main.SPEED_OF_LIGHT

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 265
Java Restore security prompts not working 10 264
maven not picking latest jar instead picking old jar from .m2 12 75
ArrayList Adding/settign data 3 38
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question