Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-11-08
2
Medium Priority
?
31 Views
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?
0
Comment
Question by:srikotesh
[X]
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
2 Comments
 
LVL 28

Accepted Solution

by:
dpearson earned 1000 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,

Doug
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1000 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
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

688 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