2

If I put constant variable values directly in classes, its panic when this value get changes. If i make a constant class to keep such constants it helps to do changes in one class only. But again I need to identify all the classes which are using these constants. Because recompilation is required.

then what is the need of separate constant class? suggest me another way to manage constants, if better.

3
  • why does a value change for a constant???? Sep 30, 2011 at 9:47
  • Surely your constants shouldn't change enough for this to be an issue. If they do then it sound like they aren't constants...
    – Chris
    Sep 30, 2011 at 9:48
  • It could be change due to bad design, or when 2 separate teams are working on a same application. so due to miscommunication, or might be due to project need in future to make some values meaning ful or with some other reason
    – noquery
    Sep 30, 2011 at 9:52

3 Answers 3

5

Use property file instead of making constant class

2
  • 1
    grt, how i missed it... vry quick reply.. but it'll increase IO operation.
    – noquery
    Sep 30, 2011 at 9:46
  • 1
    no its not. You just read file in static file. and use variables throughout the application. Sep 30, 2011 at 9:47
1

I would NEVER use a class for all of my constants.

I would also NEVER use a class for all my integers, or for all strings, or one class for all final variables and another class for all static variables, etc.

Group your members by function, not by a language feature.

If i make a constant class to keep such constants it helps to do changes in one class only.

Following that logic, you could put your whole project in a single class.

4
  • Your answer is pretty unclear to me. I dint ask to group constants. I do as per my project and the requirement arise
    – noquery
    Sep 30, 2011 at 10:01
  • Apologies if I misunderstood your question. I thought you suggested to have a single class in your project where you put all constants (static final variables). I have seen this in various projects, and think it is bad practice.
    – michael667
    Sep 30, 2011 at 10:09
  • 1
    About property files: You will lose compile-time type checking for everything you put in a property file. Some things must go into property files, but I have seen projects overusing them.
    – michael667
    Sep 30, 2011 at 10:14
  • 1
    @michael667, I am agree with your last statement but property files must be used to keep constant values. How will these values be checked at compilation time? Compiler will check for syntaxes and the data type. And the values which are stored in property file will have String type by default. Which require your intervention for casting. Oct 1, 2011 at 4:45
0

I'm pretty new to JAVA and not sure to understand your question very well, but what about something like a file Points.java (for the example, but you can make this for Strings, int, Objects... or any types) where you have :

package classes;

import java.awt.Point;

public enum Points {
    FIRST_POSITION(0,50,50),SECOND_POSITION(1,150,200),THIRD_POSITION(2,175,200),FOURTH_POSITION(3,175,200);
    private int index;
    private Point point;

    private Points(int index,int x, int y){
        this.index = index;
        this.point = new Point(x, y);
    }

    public int getIndex() {
        return index;
    }

    public Point getPoint() {
        return point;
    }  

}

You may use multiple constructors for different properties (but I'm not sure that this last possibility could be a good technique since certain getters may differ in names...) And you can retrieve values everywhere like :

System.out.println(Points.FIRST_POSITION + ", index[" + Points.FIRST_POSITION.getIndex() + "]" + ", position:" + Points.FIRST_POSITION.getPoint());
System.out.println(Points.SECOND_POSITION + ", index[" + Points.SECOND_POSITION.getIndex() + "]" + ", position:" + Points.SECOND_POSITION.getPoint());
System.out.println(Points.THIRD_POSITION + ", index[" + Points.THIRD_POSITION.getIndex() + "]" + ", position:" + Points.THIRD_POSITION.getPoint());
System.out.println(Points.FOURTH_POSITION + ", index[" + Points.FOURTH_POSITION.getIndex() + "]" + ", position:" + Points.FOURTH_POSITION.getPoint());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.