This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes javax.swing.SwingConstants why an interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "javax.swing.SwingConstants why an interface?" Watch "javax.swing.SwingConstants why an interface?" New topic

javax.swing.SwingConstants why an interface?

Sharif Muhammad

Joined: Mar 01, 2002
Posts: 2
why are constants usually declared in an interface like in javax.swing.SwingConstants? is there any performance related gains?
Greg Brouelette
Ranch Hand

Joined: Jan 23, 2002
Posts: 144
It may have to do with the fact that multiple inhieritence is not allowed. For example, you may be writing a new Swing component that extends JComponent and you also want to have access to those constants.
So you would extend JComponent and implement SwingConstants. That way you'd have access to all the methods in JComponent as well as all the constants in SwingConstants.

For a good Prime, call:<br />29819592777931214269172453467810429868925511217482600306406141434158089
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Actually this is a topic that pure Object Oriented programmers LOVE to argue over .
Some say that putting the constants in an interface is quick easy and intuitive.
(ex: Michael Ernest in this post)
Others say that Interfaces should ONLY be used to declare TYPEs and that using it for constants is a dirty way of doing what SHOULD be done through static variables, as is done in the Math class.
(ex: see "Effective Java" - Josha Block, p 89 Item 17 "Use interfaces only to define types")
Some Pros
subject: javax.swing.SwingConstants why an interface?