Solved

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

Posted on 2016-11-08
2
28 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 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,

Doug
0
 
LVL 86

Assisted Solution

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

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

630 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