Re: Using an enum in a constructor
On Sep 20, 10:33 pm, Wojtek <nowh...@a.com> wrote:
Given the following:
---------------------------------
public class Foo
{
private static final int DEFAULT_LENGTH = 30;
private Type ivType;
private int ivLength;
public enum Type
{
OTHER,
FIXED,
VARIABLE;
}
public Foo(Type type)
{
this(type,DEFAULT_LENGTH);
}
public Foo(Type.VARIABLE varType, int length)
{
this(varType,length);
}
private Foo(Type type, int length)
{
super();
ivType = type;
ivLength = length;
}}
---------------------------------
The compiler complains that Type.VARIABLE cannot be used. Obviously
what I want is that if the Type is VARIABLE, then I want the length in
the constructor, otherwise I will use the default length.
And yes I know I can have a constructor that only takes (int length)
and then assume that the Type is VARIABLE. That is not the point here.
--
Wojtek :-)
So what about this then
public class Foo
{
private static final int DEFAULT_LENGTH = 30;
private Type ivType;
private int ivLength;
public enum Type
{
OTHER,
FIXED,
VARIABLE;
}
public Foo(Type type)
{
this(type,DEFAULT_LENGTH);
}
public Foo(Type type, int length)
{
ivType = type;
switch(type) {
case VARIABLE:
ivLength = DEFAULT_LENGTH;
break;
default:
ivLength = length;
}
}
}
Steve