9

Internally for Java what is better, or what is optimal, or What is the standard: implement a class with constants or using dot notation?

Example:

Option 1:

import com.myproject.Constantes;

public class myClass {
    myClass() {
        System.out.println("Math: " + Constantes.PI);
    }
}

Option 2:

import com.myproject.Constantes;

public class myClass implements Constantes {

    myClass() {
        System.out.println("Math: " + PI);
    }
}

Which is better and why? MVJ use, resources, speed?

9
  • 3
    programmers.stackexchange.com/questions/49572/… Commented Oct 29, 2012 at 10:49
  • neither one nor two. i dont like both solutions. with this you will have alwys a direct dependency to this constants class. put the constants to the class where they belong to technically/functional Commented Oct 29, 2012 at 10:50
  • @resTive It looks like that class really needs that PI there. So how would you eliminate that dependency with your #3? Commented Oct 29, 2012 at 10:53
  • static imports are optimal in several ways I guess.. Commented Oct 29, 2012 at 10:54
  • Implementing an interface to get access to constants is called the constant interface antipattern. The name "antipattern" suggests that this is bad practice. Commented Oct 29, 2012 at 10:56

6 Answers 6

9

If Constantes is purely a collection of constants (as the name implies) and doesn't define functionality that you need to expose in myClass, then A) It shouldn't be an interface, and B) You shouldn't implement/extend it. Import it and use it as in Option 1.

Sign up to request clarification or add additional context in comments.

3 Comments

actually, I've found an interface that only has constants varied. belongs to no structure, no methods, only variables. It's like a drawer disaster. And I've found a class that implements that interface but is repeated more than 100 times the code "Constantes.name_constant" and I thought "better implement and replace all that code repeated (Constantes.name_constant) and left alone (name_constant)".
@user1782602: It's your decision. I wouldn't, I'd leave the Constantes.name_constant usage in the code. It's nice and clear, for one thing, and it doesn't say your class implements something that it doesn't really implement. In terms of the JVM, I don't think it cares one way or the other.
Understood, must respect a model and make good use of the class hierarchy. Here are misusing and I agree with your way of doing it. Thank you very much!
4

I think OPTION 1 should be used to avoid mistaking another PI defined internally in the current class.

Comments

3

Option 1 should be used, because this will definetly use the constant defined in the imported class.

If you had a local variable called PI in myClass, Option 2 would you that variable, instead of the one in the imported class.

Comments

1

You're doing two different things here. In the first fragment you're just writing code that happens to reference stuff in the Constantes class/interface and thus needs to be import'ed whereas in the second fragment, you're stating the your code must conform to the Constantes interface, i.e. implementing any and all methods therein.

Cheers,

Comments

1

implements (an interface, not a class) says that myClass must honour the contract specified by Constantes (usually with some method specifications that must be implemented in your class).

Please, check about Object Oriented Programming (Programación Orientada a Objetos) concepts before getting into the particularities of any given language.

3 Comments

actually, I've found an interface that only has constants varied. belongs to no structure, no methods, only variables. It's like a drawer disaster. And I've found a class that implements that interface but is repeated more than 100 times the code "Constantes.name_constant" and I thought "better implement and replace all that code repeated (Constantes.name_constant) and left alone (name_constant)".
Ok, I misunderstood your question. Then Peter Lawrey's answer should be the correct, mark it as such.
No, your answer is correct. I understand perfectly and try to apply it. I'm trying to improve this project gradually Thanks! :)
1

Often clarity is more important than performance and this is no exception.

Option 1 is preferred to option 2 as the latter implies that myClass is a Constantes which doesn't make sense.

Since Java 5.0 you have another option which may be better.

import static com.myproject.Constantes.PI;
// OR
import static com.myproject.Constantes.*;

public class MyClass{
  MyClass(){
       System.out.println("Math: " + PI);
  }
}

2 Comments

Thank you! I'm done with a project 1.4: (I'm afraid I can not change it but I'm also in another 1.5 and try to apply it, I did not know that this could be done.
Its worth nothing that the End of Public Updates for Java 6 has been extends to Feb 2013. blogs.oracle.com/henrik/entry/java_6_eol_h_h If you get a chance to update, I would seriously consider Java 7 not Java 5.0.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.