oop - Java: Definition of methods and variables inside enum's constant -


i doing experiments , accidently wrote code, weird , don't all. surprised can compile it. looks this:

enum foo {     value_1 {         public int myvariable = 1;     },     value_2 {         public void mymethod() {             //         }     },     value_3; } 

as expected, it's not possible access such element in following way:

foo.value_2.mymethod(); 

the reason is, compiler method inside enumeration itself.

i presumed it's not possible access these methods , variables outside enumeration. reason, tried create parametric constructor , call internal variable:

enum foo {     value(internalvariable) {         int internalvariable = 1;     };      private foo(int param) {         //     } } 

it wasn't possible compile such construction. thinking what's point of defining inside constant if there no way access it.

i trying create same-named methods in constant in enumeration check out if collides in way. didn't!

enum foo {     value_1 {         int myvariable = 1;          public int mymethod() {             return myvariable;         }     },     value_2 {         //     };      public int mymethod() {         return 0;     } } 

and here comes funny moment! tried proceed call of mymethod() inside enumeration , figured out how java magic works. methods, defined inside constant, overrides methods defined inside enumeration.

foo.value_1.mymethod(); // returns 1 foo.value_2.mymethod(); // returns 0 

however, can't override variable, right? curious, how works variables only.

enum foo {     value_1 {         public int myvariable = 1;     },     value_2 {         //     };      public int myvariable = 0; }  ....  system.out.println(foo.value_1.myvariable); // returns 0 system.out.println(foo.value_2.myvariable); // returns 0 

now i'm getting questions:

  1. why don't error if create public method inside constant , left enumeration empty without method? in case, method defined can't called @ all. or wrong?

    update: know enumeration can implement interface. however, if haven't said that, whole code pointless.

    someone pointed out if method can't accessed language in normal way, it's still possible use reflection. well... why don't design inaccessible keyword?

    inaccessible void magicalmethod() {      // } 

    such method compiled *.class file. when want use it, you've load bytecode , interpret it.

    i can't understand, why it's possible define unreachable method. reason can think programmer working , doesn't have definition of interface yet. he's preparing code of single methods , add "implements" keyword later. beside illogical, still require have such method in constants.

    i think should end error, not warning unused method. may forget add "implement" clause or define method in enumeration (which overridden) , realize after first use. java strict language, i'd expect behavior.

  2. why don't error if create public variable (or field, more precise) inside constant? can't accessed in case (from outside). therefore, modifier "public" doesn't make sense here.

    update: it's more less same thing in previous point, except visibility modifier useless here. doesn't matter if it's public, protected or private, because won't able access anyway. think bug.

  3. why it's possible define class (without visibility modifiers), not interface? yeah, wouldn't want write brutal enumeration need define classes inside constant , use inheritance there. if it's possible define classes , abstract classes, seems little weird.

    update: not you'd need on regular basis, understand might useful. why it's limited classes , interfaces can't defined well?

    enum foo {     value {         class myclass {             // ok         }          abstract class myabstractclass {             // ok         }          interface myinterface {             // fail. won't compile.         }     } } 
  4. did use such functionality somewhere? can imagine might useful, it's little confusing. also, when searching resources that, didn't find anything.

    update: i'd see practical example wth overridden methods in enum constant class body. have seen in open-source project?

environment:

$ java -version java version "1.7.0_21" openjdk runtime environment (icedtea 2.3.9) (7u21-2.3.9-0ubuntu0.12.10.1) openjdk 64-bit server vm (build 23.7-b01, mixed mode) 

thanks time , answers!

why don't error if create public method inside constant , left enumeration empty without method? in case, method defined can't called @ all. or wrong?

in fact compiler should able see method isn't visible outside enum constant's class body , warn if isn't used - know sure eclipse this. dasblinkenlight points out, such public method may in fact override of method declared interface enum implements.

i can't understand, why it's possible define unreachable method. reason can think programmer working , doesn't have definition of interface yet. he's preparing code of single methods , add "implements" keyword later. beside illogical, still require have such method in constants.

as noted, doesn't apply enum constant classes. there many scopes - private nested classes, local classes, anonymous classes - it's pointless member public.

the problem question language designers answer it. can give opinion, is: why should error? language spec doesn't come free - in jls must painstakingly defined, , implemented , tested. real question is, benefit there making error? mere fact while unused member might indicate bug (hence warning), isn't hurting anything.

why don't error if create public variable (or field, more precise) inside constant? can't accessed in case (from outside). therefore, modifier "public" doesn't make sense here.

same above - compiler or @ least ides warn if variable isn't used. same if declared public variable in private nested class , didn't reference anywhere. in case, it's not priority of jls forbid such situations, notwithstanding eye of reflection sees all.

it's more less same thing in previous point, except visibility modifier useless here. doesn't matter if it's public, protected or private, because won't able access anyway. think bug.

here, you're forgetting members might still used within enum constant class body - think of helper method example. it's in case access modifiers don't matter , can left off.

why it's possible define class (without visibility modifiers), not interface? yeah, wouldn't want write brutal enumeration need define classes inside constant , use inheritance there. if it's possible define classes , abstract classes, seems little weird.

this question, , took me while understand mean. clarify, you're saying in situation class allowed:

value_1 {     class bar { }     interface baz { } }, 

to shed light on this, try making class static:

value_1 {     static class bar { }     interface baz { } }, 

now neither allowed. why? nothing static can declared in enum constant body, because body in context of instance of constant. similar being in scope of inner (non-static nested) class:

class outer {      class inner {         // nothing static allowed here either!     } } 

static variables, methods, classes, , interfaces (which implicitly static when nested) forbidden in such scope.

did use such functionality somewhere? can imagine might useful, it's little confusing. also, when searching resources that, didn't find anything.

it's unclear functionality referring here. please update question specify you're looking - overridden methods in enum constant class body? fields? helper methods? helper classes? please clarify.