Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-11-08
2
Medium Priority
?
32 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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

926 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