Declaring members for Interfaces

T

Todd

Wojtek,

Thank you!! A valid reason to use the enum. How do you use your
enum? I am confused as to the structure to access the value
unless it's storage is public or you use an accessor. In other
words, is this how you do it?

public enum NonOrdinal
{
RADIUS( 21 ),
DIAMETER( 42 );

NonOrdinal( double value )
{
this.value = value;
}

public double value;
}

or

public enum NonOrdinal
{
RADIUS( 21 ),
DIAMETER( 42 );

NonOrdinal( double value )
{
this.value = value;
}

public double getValue()
{
return value;
}

private double value;
}

or some other way? I know that one must have at
least one enumerated variable declaration in an enum,
and I think that is confusing me some.

Wojtek, I am curious,
Todd
 
T

thufir

Next, I moved the Planet enum to its own file, away from my test class.
Now, I can not access the private member directly. So, I am back to
asking: "how do I access a private member without an accessor?"

So far as I know, without Reflection, you can't. Which leads me back to
stating that there will need to be an accessor in an enum to get at
non-ordinal values.


If I can go on a tangent: I kinda see the value of not being able to
access private members -- for security. Does C++ also have this concept?


-Thufir
 
L

Lasse Reichstein Nielsen

Todd said:
utility class: public class, private constructor,
static members

That is what an enum is (or possibly protected constructor, if
the values are separate classes).
The "enum" keyword is just syntactic sugar for the traditional
way of implementing object enums:

public class MyEnum {
public static final MyEnum FOO = new MyEnum(1,"foo");
public static final MyEnum BAR = new MyEnum(2,"bar");
private final String name;
private final int number;
private MyEnum(int number, String name) {
this.name = name;
this.number = number;
}
public String getName() { return name; }
public int getNumber() { return number; }
}

/L
 
L

Leonard Milcin

Lew said:
Hooray, type safety!

This is fascinating how thinking about type safety changes with
experience of a developer. Young, less experienced are quick to adapt
scripting languages where there is almost no type safety and all the
freedom of doing what you want.

Aside of usual arguments about maintaining large applications, etc. I've
found one good argument to show that type safety is very good to have
and use. It's the type safety that let's your IDE perform most of the
static analysis of your code. Show them some refactorings or other neat
but standard features in IDEA or even in Eclipse (find usages...) and
then ask to do the same for Perl or PHP;-)

Best regards,
Leonard Milcin
 
W

Wojtek

Todd wrote :
public enum NonOrdinal
{
RADIUS( 21 ),
DIAMETER( 42 );

private double value; private NonOrdinal( double value )
{
this.value = value;
}

public double getValue()
{
return value;
}

public NonOrdinal getNonOrdinal(double value) throws
EnumNotFoundException
{
for (NonOrdinal ord : NonOrdinal.values())
if ( ord.getValue == value )
return ord;

throw new EnumNotFoundException("NonOrdinal not found: " +
value);
}

Is how I do it, though I tend to use an int for my "value" for
persistance. You will need to create your own EnumNotFoundException.

Um, don't get confused with the NonOrdinal.values() as the values()
returned are NOT your private value attribute.
 
L

Leonard Milcin

Wojtek said:
Todd wrote :


private double value;
private NonOrdinal( double value )

public NonOrdinal getNonOrdinal(double value) throws
EnumNotFoundException
{
for (NonOrdinal ord : NonOrdinal.values())
if ( ord.getValue == value )
return ord;

throw new EnumNotFoundException("NonOrdinal not found: " + value);
}

OMG! I won't even comment. This is just silly... don't teach people if
you can't solve problems by yourself.

If you really, really need such a mechanism, try this:

public enum Test {

A1( 1 ),
A2( 2 );

private final Integer value;

private static final Map<Integer, Test> byValue;

static {
byValue = new HashMap<Integer,Test>();
for (Test t : Test.values()) {
byValue.put(t.value, t);
}
}

private Test(Integer value) {
this.value = value;
}

private Integer getValue() {
return value;
}

public static Test getByValue(Integer value) {
return byValue.get(value);
}

}

Regards,
Leonard
 
L

Leonard Milcin

Wojtek said:
Todd wrote :


private double value;
private NonOrdinal( double value )

public NonOrdinal getNonOrdinal(double value) throws
EnumNotFoundException
{
for (NonOrdinal ord : NonOrdinal.values())
if ( ord.getValue == value )
return ord;

throw new EnumNotFoundException("NonOrdinal not found: " + value);
}


Is how I do it, though I tend to use an int for my "value" for
persistance. You will need to create your own EnumNotFoundException.

Um, don't get confused with the NonOrdinal.values() as the values()
returned are NOT your private value attribute.

public enum Test {

A1(1),
A2(2);

private static final Map<Integer, Test> byValue;

static {
byValue = new HashMap<Integer, Test>();
for (Test t : Test.values()) {
byValue.put(t.value, t);
}
}

public static Test getByValue(Integer value) {
return byValue.get(value);
}

private Test(Integer value) {
this.value = value;
}

private final Integer value;

public Integer getValue() {
return value;
}
}

Regards,
Leonard
 
W

Wojtek

Leonard Milcin wrote :
public static Test getByValue(Integer value) {
return byValue.get(value);
}

But now the calling routine must test for a null, whereas throwing an
exception can be caught where needed.
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top