Why I don't get any error if I create public method inside the constant and left enumeration empty without this method? In that case, the method I just defined can't be called at all. Or am I wrong?
In fact the compiler should be able to see that the method isn't visible outside the enum constant's class body and warn you if it isn't used - I know for sure that Eclipse does this. As dasblinkenlight points out, such a public method may in fact be an override of a method declared by an interface that the enum implements.
I just can't understand, why it's possible to define unreachable method. The only reason I can think is that programmer is working and doesn't have definition of interface yet. So he's just preparing code of single methods and will add "implements" keyword later. Beside this is illogical, it would still require to have such a method in all constants.
As I already noted, this doesn't specifically apply to enum constant classes. There are many scopes - private nested classes, local classes, anonymous classes - where it's pointless for a member to be public.
The problem with this question is that only the language designers could truly answer it. I can only give my opinion, which is: why should it be an error? The language spec doesn't come for free - everything in the JLS must be painstakingly defined, and then implemented and tested. The real question is, what benefit is there to making it an error? The mere fact is that while an unused member might indicate a bug (hence the warning), it isn't hurting anything.
Why I don't get any error if I create public variable (or field, to be more precise) inside the constant? It can't be accessed in the any case (from the outside). Therefore, modifier "public" doesn't make any sense here.
Same as above - the compiler or at least some IDEs will warn you if a variable isn't used. This is the same as if you declared a public
variable in a private
nested class and then didn't reference it anywhere. In any case, it's not the priority of the JLS to forbid such situations, notwithstanding that the eye of reflection sees all.
It's more less the same thing as in the previous point, except the visibility modifier is completely useless here. It really doesn't matter if it's public, protected or private, because you won't be able to access that anyway. I think this is a bug.
Here, you're forgetting that members might still be used within the enum constant class body - think of a helper method for example. It's just that in this case access modifiers simply don't matter and can be left off.
Why it's possible to define a class (without visibility modifiers), but not interface? Yeah, you wouldn't probably want to write so brutal enumeration that you would need to define classes inside the constant and even to use inheritance there. But if it's possible to define classes and abstract classes, it seems little weird.
This is a good question, and it took me a while to understand what you mean. To clarify, you're saying that in this situation only the class is allowed:
VALUE_1 {
class Bar { }
interface Baz { }
},
To shed light on this, try making the class static
:
VALUE_1 {
static class Bar { }
interface Baz { }
},
Now neither are allowed. Why? Nothing static
can be declared in an enum constant body, because the body is in the context of the instance of that constant. This is similar to being in the scope of an inner (non-static nested) class:
class Outer {
class Inner {
// nothing static allowed here either!
}
}
Static variables, methods, classes, and interfaces (which are implicitly static when nested) are all forbidden in such a scope.
Did you use such a functionality somewhere? I can imagine it might be useful, but it's little confusing. Also, when I was searching for some resources about that, I didn't find anything.
It's unclear what functionality specifically your referring to here. Please update the question to specify what exactly you're looking for - overridden methods in an enum constant class body? Fields? Helper methods? Helper classes? Please clarify.