• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 24204
44
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: 76872
366
  • 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: 76872
366
  • 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
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic