Solved

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

Posted on 2016-11-08
2
18 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 26

Accepted Solution

by:
dpearson earned 250 total points
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now