Reputation: 39
Sometimes I saw use static enum in android. but I can't find that information(I know enum in C) example,
public static enum brush{
static{
array[0] = brush1;
array[1] = brush2;
array[2] = brush3;
array[3] = brush4;
}
}
but that is occurred error in project. error message is "Syntax error, insert "Identifier" to complete EnumConstantHeader" but I don't understand that's mean.
Upvotes: 2
Views: 5813
Reputation: 8946
I don't think it is the correct implementation of enum type in java. You should use enum types any time you need to represent a fixed set of constants.
syntax :
access-modifier enum enum-name
{
enum_type_1,
enum_type_2 //(optioanl semi colon)
}
Your enum
more look like some static method again with some static block if we remove the enum
keyword from it. Can't really understand the logic behind it. May be you are thinking to use the enum
inside some class but nested enum declarations are implicitly static.
Upvotes: 0
Reputation: 23344
A Java enum "is a type whose fields consist of a fixed set of constants ... you should use enum types any time you need to represent a fixed set of constants."
As specified in Java Language Specification, Section 8.9:
Enum types must not be declared abstract; doing so will result in a compile-time error. An enum type is implicitly final unless it contains at least one enum constant that has a class body. It is a compile-time error to explicitly declare an enum type to be final. Nested enum types are implicitly static. It is permissible to explicitly declare a nested enum type to be static. This implies that it is impossible to define a local enum, or to define an enum in an inner class. The direct superclass of an enum type named E is Enum. An enum type has no instances other than those defined by its enum constants. It is a compile-time error to attempt to explicitly instantiate an enum type.
Because a Java enum type is good for defining a fixed set of constants, it's often used to represent constants like these in Java programs:
Typical examples:
1) Declares the days of the week in an enum named Day.
public enum Day
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
2) Declares the margin types that you can use when working with CSS:
/**
* Corresponds to CSS margins.
*/
public enum Margin
{
TOP, RIGHT, BOTTOM, LEFT
}
So as you saw, it's worth nothing that because these are constants, we name them with all capital letters, which is consistent with Java naming conventions.
Simple example -
public class JavaEnumToStringExample
{
public static void main(String[] args)
{
// loop through the enum values, calling the
// enum toString method for each enum constant
for (Day d: Day.values())
{
System.out.println(d);
}
}
}
enum Day
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
And the output -
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
Enum idioms-
Upvotes: 2
Reputation: 1500675
The problem is that this is an enum without a list of members. You'd normally have:
public enum Foo {
VALUE1, VALUE2;
}
You can have an enum with no members, but you still need the semi-colon:
public enum Foo {
;
}
That's now valid, although not terribly useful.
The static
part in the enum declaration is presumably because it's nested within another class. This isn't actually required - nested enum
declarations are implicitly static.
The static { ... }
is just a static initializer, run when the type is initialized.
Enums in Java are quite different from those in C. You should read the enum
section on the Java tutorial for more details.
Upvotes: 10
Reputation: 30528
You are missing an }
from the static
block declaration:
public static enum brush{
static{
array[0] = brush1;
array[1] = brush2;
array[2] = brush3;
array[3] = brush4;
}
}
There are several code smells here:
brush
does not adhere to naming conventions: it should be Brush
array
is not a meaningful name and I think it is also a reserved workd if I remember right. You should choose something which tells about its functionbrush1
is not so meaningful eitherarray
's declarationWhat you are most likely try to achieve is something like this:
public static enum Brush{
BRUSH_1, BRUSH_2, BRUSH_3, BRUSH_4;
}
Upvotes: 3