Forums Register Login

What is an interface what has only constants called?

+Pie Number of slices to send: Send
Is there any special name to that interface(an interface with public constants)? I was asked this in an interview.

Thanks
+Pie Number of slices to send: Send
I call it bad code.
+Pie Number of slices to send: Send
 

Originally posted by Ken Blair:
I call it bad code.



Indeed, Ken is in good company here. There's no word for that kind of interface, specifically, but doing things that way has been called the "Constant Interface Antipattern."
+Pie Number of slices to send: Send
Thanks Ernest.
+Pie Number of slices to send: Send
Is keeping constants in classes and using them elsewhere also a bad practise? I am talking about making classes soley for keeping constants...

Thanx in Advance,

Maki Jav
+Pie Number of slices to send: Send
 

Originally posted by Maki Jav:
Is keeping constants in classes and using them elsewhere also a bad practise? I am talking about making classes soley for keeping constants...

Thanx in Advance,

Maki Jav



I would say it smells real bad. Why would you need to do that?
+Pie Number of slices to send: Send
Is there anything wrong with keeping constants in a class to use elsewhere?

In my opinion, no, provided you call your class an enum . . .

CR
+Pie Number of slices to send: Send
 

Originally posted by Campbell Ritchie:
Is there anything wrong with keeping constants in a class to use elsewhere?

In my opinion, no, provided you call your class an enum . . .

CR


+Pie Number of slices to send: Send
How is it worse then passing needed objects all over the place, just because they contain needed constants.

There is a reason many java API classes have public static final variables.

Have an interface may not be the best way to go about it, but it is hardly crippling.
[ May 25, 2006: Message edited by: Steve L. Williams ]
+Pie Number of slices to send: Send
 

Originally posted by Steve L. Williams:
How is it worse then passing needed objects all over the place, just because they contain needed constants.

There is a reason many java API classes have public static final variables.

Have an interface may not be the best way to go about it, but it is hardly crippling.

[ May 25, 2006: Message edited by: Steve L. Williams ]



Why would you be passing objects around just because they have a constant to begin with? You don't have to pass anything to have access to a public static variable.

Developers use a constant interface because they want to inherit the constants. That's not what an interface was meant for and it needlessly attaches an interface with no contract to your class.
+Pie Number of slices to send: Send
Well... It is always a nice to have if you place and organize constants at one place. It makes your code maintainable. I guess having a class in a java application holding constants for all the classes is better.

public class MyAppConstants
{
public static final class ModuleOne
{
public static final String ABC = "some text";
public static final String XYZ = "some other text";
.....................................
.....................................
}

public static final class ModuleTwo
{
public static final String OOO = "XXXXXXXX";
public static final String PPP = "xxxxxxxx";
.........................
.........................
}
............................
............................
}
+Pie Number of slices to send: Send
I take your point, Amit Srivastava, but I hope you don't try to compile anything with that many public classes in.

CR
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1224 times.
Similar Threads
Interface Constant or class based ?
Regarding Constant Interface
Implementing interface with no methods
Enum vs Constant File
interface having constants
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 23:54:57.