Enum: how to call an enum constant's own method?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Enum: how to call an enum constant's own method?

Hybrid View

  1. #1
    Join Date
    Jun 2010
    Posts
    3

    Enum: how to call an enum constant's own method?

    Hi,
    I'm a Java and Codeguru newbie, so please don't be too harsh.
    I'm currently having this issue with enum: I'm creating an enum type that has several enum constants, one of which has its own method like this:

    public enum EnumType {
    ENUMCONSTANT1,
    ENUMCONSTANT2,
    ENUMCONSTANT3 { void constant3sOwnMethod() { } }

    // enum type's constructor and other methods
    ...
    }

    My question is: how do I call constant3sOwnMethod() ?
    If I don't use this method anywhere it compiles fine. But if I add a line to call the method such as this:
    EnumType.ENUMCONSTANT3.constant3sOwnMethod();
    it won't compile, but instead shows error message: "cannot find method constant3sOwnMethod()"

    Please help!!!

    P.S. Sorry I'm still trying to figure out how to post indented source code.

  2. #2
    Join Date
    Jun 2010
    Posts
    3

    Re: Enum: how to call an enum constant's own method?

    I think I know how to show code now:
    Code:
    public enum EnumType {
      ENUMCONSTANT1,
      ENUMCONSTANT2,
      ENUMCONSTANT3 { 
        void constant3sOwnMethod() { 
        } 
      }
    
      // enum type's constructor and other methods
      ...
    }
    Hope this will work

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Enum: how to call an enum constant's own method?

    It will compile, but you won't be able to call that method on ENUMCONSTANT3. The reason is that enum constants are secretly anonymous subclasses of the enum type, so you can only call methods on them that are declared (and optionally implemented) in the enum type or overridden/implemented in the enum constant 'subclass'.

    This means you will need a method in EnumType that is overridden or implemented in the enum constants. If it is abstract, all the constants will have to implement it:
    Code:
    static enum EnumType {
       ENUMCONSTANT1 { void constantsOwnMethod() { ... } },
       ENUMCONSTANT2 { void constantsOwnMethod() { ... } },
       ENUMCONSTANT3 { void constantsOwnMethod() { ... } };
    
       abstract void constantsOwnMethod(); // implement in constants
    }
    Alternatively, you can provide a default implement of the method in EnumType and override it in ENUMCONSTANT3:
    Code:
    static enum EnumType {
        ENUMCONSTANT1,
        ENUMCONSTANT2,
        ENUMCONSTANT3 { void constantsOwnMethod() { /* overriding implementation */ }};
    
        void constantsOwnMethod() { /* default implementation */ }
    }
    Until real software engineering is developed, the next best practice is to develop with a dynamic system that has extreme late binding in all aspects...
    A. Kay
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Jun 2010
    Posts
    3

    Re: Enum: how to call an enum constant's own method?

    Dlorde: thanks!!

    But I'm still a bit confused.
    The way I see it (which may be wrong :-) ) is ENUMCONSTANT1 and ENUMCONSTANT2 are named objects of type EnumType, and ENUMCONSTANT3 is a named object of an anonymous class that extends from EnumType (hence generated the EnumType$1.class file).

    So if you call
    Code:
      EnumType.ENUMCONSTANT3.constant3sOwnMethod();
    you're actually calling the method on an object (ENUMCONSTANT3), not a (static) method on a class.
    We have an object (ENUMCONSTANT3) to get a hold of.
    The object's class is compiled, so the method should be "registered" to JVM.

    Maybe Java just doesn't allow us to call any method of an anonymous class (except those inherited)?

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Enum: how to call an enum constant's own method?

    Quote Originally Posted by DivideByZero View Post
    So if you call
    Code:
      EnumType.ENUMCONSTANT3.constant3sOwnMethod();
    you're actually calling the method on an object (ENUMCONSTANT3), not a (static) method on a class.
    I don't know what 'static' has to do with it, but yes, in principle it is a method call. However, you can only access anonymous class objects via a reference to the superclass (EnumType), so the only methods accessible are those declared in the superclass. You can't cast to the subclass to call its methods because it's not a named type - it has no type name to cast to. There's nothing to stop you putting whatever methods you like in the anonymous class, but unless they implement or override methods declared in the superclass, they will only be accessible from within the subclass.

    Maybe Java just doesn't allow us to call any method of an anonymous class (except those inherited)?
    It's not a question of 'not being allowed', it's just a question of accessibility. You can call any method of the anonymous class from within that anonymous class, but its effective public interface is necessarily the public interface of the superclass.

    The outcome of any serious research can only be to make two questions grow where only one grew before...
    T. Veblen
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center