Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3307252
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:23:03+00:00 2026-05-17T21:23:03+00:00

In Java, with Sun’s JDK 1.6, with an enum such as this: public enum

  • 0

In Java, with Sun’s JDK 1.6, with an enum such as this:

public enum MyEnum {
    FIRST_MEMBER { public void foo() { } },
    SECOND_MEMBER { public void foo() { } }, 
    THIRD_MEMBER { public void foo() { } };
}

The compiled files are:

MyEnum$1.class  MyEnum$2.class  MyEnum$3.class  MyEnum.class 

This also means that a stack trace showing foo(), or the method call printed in JVisualVM, etc., will have something like this at the top:

mypackage.MyEnum$1.run()

The $1 in the class name is due to the members of the enum being compiled to anonymous inner classes. I am wondering if it is safe to assume that the numbers used in these class names map to the order in which the enum members are defined? If it is not, is there a standard, guaranteed way to find the enum member from the number used as the anonymous class name?


EDIT

In regards to the design of the enum, this was used for illustration purposes only. The real enum implements an interface, and each member provides a different implementation of the methods. Please don’t pay too much attention to what admittedly looks a bit strange.


EDIT #2

To clarify, I am not trying to do anything with this information programmatically (such as weird reflection nonsense). Rather, I am looking at stack traces and profiling information, and trying to map a method call on an enum member (shown as a call on an anonymous class) against the actual enum member in the source code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:23:03+00:00Added an answer on May 17, 2026 at 9:23 pm

    In stacktraces, you’ll also get the line number in the source file. Assuming you have the source, that’ll reveal which constant it is. (In eclipse, simply click the line number in the console view to directly navigate to the source).

    To find the constant name for a class, you could grab the class file for the enum, and disassemble the static initializer. For instance, if you compile:

    enum PieceColor {
        Black {
            @Override public String toString() { return "dark";}
        },
        White {
            @Override public String toString() { return "light";}       
        }
    }
    

    and then do:

    javap -c PieceColor
    

    you get:

    static {};
      Code:
       0:   new #13; //class tools/PieceColor$1
       3:   dup
       4:   ldc #15; //String Black
       6:   iconst_0
       7:   invokespecial   #16; //Method tools/PieceColor$1."<init>":(Ljava/lang/String;I)V
       10:  putstatic   #20; //Field Black:Ltools/PieceColor;
       13:  new #22; //class tools/PieceColor$2
       16:  dup
       17:  ldc #24; //String White
       19:  iconst_1
       20:  invokespecial   #25; //Method tools/PieceColor$2."<init>":(Ljava/lang/String;I)V
       23:  putstatic   #26; //Field White:Ltools/PieceColor;
    

    But there might be a more elegant method, but if all else fails, this should do the trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The top of my web.xml file looks like this: <?xml version=1.0 encoding=UTF-8?> <web-app xmlns=http://java.sun.com/xml/ns/j2ee
I'm not quite sure if this is specific to Sun Java Systems Application Server
I'm writing a Java application that runs on Linux (using Sun's JDK). It keeps
In this post http://java.sun.com/docs/books/tutorial/java/package/usepkgs.html into the paragraph Apparent Hierarchies of Packages is written: At
On http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html it states that: Compares this URL for equality with another object. If
I've read this: http://java.sun.com/products/javamail/javadocs/com/sun/mail/imap/package-summary.html I want to write an imap server rather than imap
Using the Sun Java VM 1.5 or 1.6 on Windows, I connect a non-blocking
Should I stick with Sun's Java code conventions for PHP code?
I read the Java tutorials on Sun for JAR files, but I still can't
I've got a somewhat dated Java EE application running on Sun Application Server 8.1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.