Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Declaring constants in class or interface - which is better?

 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • 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"
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!