• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring constants in class or interface - which is better?

 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very simple question. Since constants are "public static final" .... What are your views?
 
Alain Dickson
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The answer is as simple as your question:

Interface is a contract, If you want every class that implements this interface to have the constants, they should be in the interface else in implementing class.

"public Static final" : here, what I understand from your question is that you are concentrating on "public" part i.e. data is visible to everyone, but if you shift your attention to "final" part - no one will be able to change it. The only fundamental of having private data in a class is that no one can alter it in an inappropriate way(thats why you have getters and setters), but here it is "final"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic