What is the reason for putting constants in another class?

?

-

Does anybody know the reason why the constants in
javax.swing.ScrollPaneConstants are put in a separate class instead of
simply putting them inside javax.swing.JScrollPane itself?
 
J

Joseph Dionne

- said:
Does anybody know the reason why the constants in
javax.swing.ScrollPaneConstants are put in a separate class instead of
simply putting them inside javax.swing.JScrollPane itself?


They are all gouged in one place, typically public static final, and
accessed as MyClassDefaults.CONSTANT_VALUE.
 
?

-

Joseph said:
They are all gouged in one place, typically public static final, and
accessed as MyClassDefaults.CONSTANT_VALUE.

Then how about the constants in java.awt.event.ActionEvent? Why didn't
they put them in ActionEventConstants? Is it because they only make it
a point to separate them from swing onwards?
 
J

Joseph Dionne

- said:
Then how about the constants in java.awt.event.ActionEvent? Why didn't
they put them in ActionEventConstants? Is it because they only make it
a point to separate them from swing onwards?

Perhaps Java is showing its age? As use in a language increases,
drawing greater feed back, "proper" syntax evolves addressing common
complaints. But, Java tries to be "upwards" portable. Changing AWT at
1.3 or later would break every application developed prior. That is
what Microsoft does with annoying regularity. Sun changes API structure
breaking upward compatibility, i.e. depreciated notices, but gives
developers time to update source code while allowing them up use new
features in new product.

Joseph
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

- said:
Does anybody know the reason why the constants in
javax.swing.ScrollPaneConstants are put in a separate class instead of
simply putting them inside javax.swing.JScrollPane itself?

In this particular case, notice that ScrollPaneConstants is an
interface. Thus, by implementing ScrollPaneConstants in a class one can
write MY_FOO_CONSTANT instead of ScrollPaneConstants.MY_FOO_CONSTANT to
refer to some variable. In java 1.5, there is a facility called static
import that allows one to omit the class name without resorting to this
hack.
 
A

Andrew McDonagh

Daniel said:
In this particular case, notice that ScrollPaneConstants is an
interface. Thus, by implementing ScrollPaneConstants in a class one can
write MY_FOO_CONSTANT instead of ScrollPaneConstants.MY_FOO_CONSTANT to
refer to some variable. In java 1.5, there is a facility called static
import that allows one to omit the class name without resorting to this
hack.

And yet we should keep in mind that interfaces that define constants,
has been shown to be an anti-pattern.
 
W

Wibble

I hate this pattern. You put a pile of constants
in a class, use them globally, all the consumers
refactor away and you end up with a file full of
things nobody uses or knows what they're for, but
everyone is afraid to touch.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,834
Latest member
BuyCannaLabsCBD

Latest Threads

Top