File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes What is an interface what has only constants called? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is an interface what has only constants called?" Watch "What is an interface what has only constants called?" New topic
Author

What is an interface what has only constants called?

Sital Kotamraju
Greenhorn

Joined: Mar 21, 2006
Posts: 13
Is there any special name to that interface(an interface with public constants)? I was asked this in an interview.

Thanks
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I call it bad code.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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."


[Jess in Action][AskingGoodQuestions]
Sital Kotamraju
Greenhorn

Joined: Mar 21, 2006
Posts: 13
Thanks Ernest.
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435
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


Help gets you when you need it!
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Steve L. Williams
Greenhorn

Joined: May 20, 2006
Posts: 13
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 ]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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.
Amit K Srivastava
Greenhorn

Joined: May 16, 2006
Posts: 23
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";
.........................
.........................
}
............................
............................
}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
I take your point, Amit Srivastava, but I hope you don't try to compile anything with that many public classes in.

CR
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is an interface what has only constants called?