• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is an interface what has only constants called?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any special name to that interface(an interface with public constants)? I was asked this in an interview.

Thanks
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I call it bad code.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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."
 
Sital Kotamraju
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ernest.
 
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I take your point, Amit Srivastava, but I hope you don't try to compile anything with that many public classes in.

CR
 
reply
    Bookmark Topic Watch Topic
  • New Topic