This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Class Vs Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Class Vs Interface" Watch "Class Vs Interface" New topic
Author

Class Vs Interface

vishwa venkat
Ranch Hand

Joined: Nov 22, 2003
Posts: 185
which is best place to define some public final constants? what are things to consider?

Thanks,
-Visu
Ajith George
Ranch Hand

Joined: Dec 22, 2005
Posts: 109
You can use either an interface or class. But it depends on which you must go for. If you are using interface the advantage is that you can implement this interface on where ever you need to use those values. This method saves memory coz no object is being created here.

So if you need nice performance go for interface..................... :roll:


SCJP 1.4, Brainbench
LinkedIn - Blog
Michael Duffy
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Originally posted by Ajith George:
You can use either an interface or class. But it depends on which you must go for. If you are using interface the advantage is that you can implement this interface on where ever you need to use those values. This method saves memory coz no object is being created here.

So if you need nice performance go for interface..................... :roll:


Since public static final constants are always associated with a class, not an instance, there's no memory savings to be had by putting constants in an interface.

I think constants should be defined with the class where they are pertinent. Follow the example of Java itself. There's no single interface or class where java.lang.Math.PI and java.util.Calendar.DECEMBER are defined. They're declared with the rest of the class that makes sense. You should do the same.


%
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
While you should avoid the "constant interface anti-pattern" there are times when constants are logically part of the interface, for example, for enumerated valued pass to or returned from methods in the interface. Here's a demo using constants and enums. Either way, it makes sense to include them in the interface. (Hmmm.... maybe they should have been defined in the IceCreamCone interface, but you get the idea...)


There is no emoticon for what I am feeling!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Class Vs Interface