File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface Question

 
Rob Levo
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a bunch of constants that need to be used by many classes.
Does it make sense to create an interface with these constants even though the interface would have no methods.
I understand an interface is supposed to act as a template for some behavior, this use would be a bit more limited.
If classes need to share global variables, and an interface is not the way to go, what is typically done?
Thanks.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this related conversation in the intermediate forum. And you may want to consider that object behavior is at times preferred over state-based solutions (not to mention a lot more interesting to explore and think about).
 
David Duran
Ranch Hand
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you read through that other thread and are thinking about type safe enumerations, I think it's very important to read the following article:

http://www.javaworld.com/javaworld/javatips/jw-javatip122.html
That's why, for my purposes, I still use interfaces hahaha
 
Rob Levo
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all!!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Duran:
If you read through that other thread and are thinking about type safe enumerations, I think it's very important to read the following article:

http://www.javaworld.com/javaworld/javatips/jw-javatip122.html
That's why, for my purposes, I still use interfaces hahaha

It's certainly important to notice this problems. But you should also notice that
a) the serialization problem has a very simple solution (also presented in the article), and
b) the classloader problem should be rather rare
So, in my not so humble opinion, you shouldn't miss this powerfull pattern just because you fear that it might not be appropriate sometime in the future. YMMV
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic