V
vamsee.maha
Can anyone tell me reason:
Why the syntax for declaring members in interfaces are declared as
constants.
Why the syntax for declaring members in interfaces are declared as
constants.
Can anyone tell me reason:
Why the syntax for declaring members in interfaces are declared as
constants.
Well the usual idea is that you dont need to qualify your constants if you
put them into an interface. Assume this:
class MyConstants {
public static String MY_STRING = "foo";
}
Now if you want to use this in a class of yours:
class MyWindow extends JFrame {
private void buildUI() {
...
field.setText(MyConstants.MY_STRING);
...
}
}
If you declare the constants in an interface:
interface MyConstants {
public static String MY_STRING = "foo";
}
you can do
class MyWindow extends JFrame implements MyConstants {
private void buildUI() {
...
field.setText(MY_STRING);
...
}
}
since you inherited the constants from the interface. Note however, that
this is considered bad style by many people, as you can get into trouble
when you have the same constant name in different interfaces. Also you are
misusing the idea of interfaces for saving some typing, which can be
considered a hack. The preferred way of doing this in Java 1.5 or above is
using static imports:
import static MyConstants.*;
class MyWindow extends JFrame {
private void buildUI() {
...
field.setText(MY_STRING);
...
}
}
Jan
I assume with "member" you mean "instance variable". As an interface itself can never be instantiated there is no way an interface can have a "member" (just like an interface cannot implement a method).Can anyone tell me reason:
Why the syntax for declaring members in interfaces are declared as
constants.
Can anyone tell me reason:
Why the syntax for declaring members in interfaces are declared as
constants.
The explanation says for declaring constants we use members of
interfaces. It also says it may not be best way of declaring
constants. I feel there is one important reason why we declare members
of interfaces as constants
A constant is a final variable initialized with a compile-time
constant expression.
In fact you should use enums as much as possible because they are a much
better way of declaring constants.
Todd said:With the enum declaration of a constant, you must supply a getter
method or make the field that holds the value of the constant
public (violating encapsulation).
Todd said:How do I get the value of mass without a getter from any
of the enumerated items from outside of the enumeration?
I took a walk, but apparently am not seeing the "magic"
you claim. Help me out?
You mean reimplement Enum.ordinal()? Please.
And your other comment about "namespace pollution" is equally off the
beam. I just don't see how it applies to enums at all.
Todd said:This example is from the Sun Tutorial site:
public enum Planet {
MERCURY (3.303e+23, 2.4397e6),
VENUS (4.869e+24, 6.0518e6), ....
private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
private double mass() { return mass; }
private double radius() { return radius; } ....
How do I get the value of mass without a getter from any
of the enumerated items from outside of the enumeration?
What you quoted is not really a normal use of enums.
I am back to stating that a utility class
is better than an enum to declare constants.
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.