1

While learning java i was trolling around SO and found question 116574. The accepted answer contained some java code that i did not understand.

EDIT: The code basically looks like this:

public enum SomeEnum {

    FUN0 {
        @Override
        public void function() {
              System.out.println("0 0 0 0 0");
        }
    },
    FUN1 {
        @Override
        public void function() {
              System.out.println("1 1 1 1 1");
        }
    };

    public abstract void function();

    public static void main(String[] args) throws Exception {

            for (SomeEnum test : values()) {
                    test.function();
            }
    }
}

It looks advanced due to the organization of the code and i am so curious about it. If it is suitable for SO to ask this kind of question: Might someone be willing to explain a bit this code?

How does it come that an enum contains main? Should it not be a class? (Eclipse is not so happy about it, but runs the main function after asking to "Select Java Application") What is this construct with FUN0, FUN1?

A pointer to some tutorial text that explains this topic already make me happy.

Community
  • 1
  • 1
Matthias
  • 3,458
  • 4
  • 27
  • 46
  • Could you paste the code into your question for us? We might as well have a complete record of what you're referring to here. –  Jul 26 '12 at 09:45
  • the question was edited to contain a link into SO. That should do the trick. – Matthias Jul 26 '12 at 10:59
  • But SO is fully editable and you cannot guarantee that code will stay as it is now (granted, the question and answer you link to is quite popular, so it is highly unlikely to change). Also, having the code here makes it absolutely clear what code you are referring to, and then other users don't have to click away for this question. –  Jul 26 '12 at 11:18

1 Answers1

2

It's an enum in which each instance override the getResult method.

What you should keep in mind is that an enum is more or less just a regular class.

How does it come that FileSizeBench is an enum and not a class?

Because it is declared using the enum keyword instead of class. An enum is used because SomeEnum has a predefined (know at compile-time) number of instances.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Link removed. Does my post answer your question, or is something still unclear to you? – aioobe Aug 16 '12 at 15:26
  • Your answer may it be as short as it is has a strange undertone. Espacially the "because it was declared enum" Part. – Matthias Aug 19 '12 at 11:34