enum as interface, sub class, singleton, stat machine

Hi,

I am going through below links

http://examples.javacodegeeks.com/java-basics/data-types/page/2/
http://examples.javacodegeeks.com/java-basics/data-types/enum/enum-to-implement-an-interface/
http://examples.javacodegeeks.com/java-basics/data-types/enum/using-an-enum-as-a-state-machine/      
http://examples.javacodegeeks.com/java-basics/data-types/enum/each-enum-instance-a-different-sub-class/

I have not understood why and how and where 'enum needs to be used as interface, sub class, singleton, state machine'

Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

contactkarthiCommented:
i think you misunderstood about enum here. you should have understoold it like enum can be used to create a singleton class and so on..

please refer the below article

http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java

small snippet from the above article

Pre java5, the most simple way of creating single ton is:

public final class Foo {

    private static final Foo INSTANCE = new Foo();

    private Foo() {
        if (INSTANCE != null) {
            throw new IllegalStateException("Already instantiated");
        }
    }

    public static Foo getInstance() {
        return INSTANCE;
    }
}

Since java5 the best way to do it is to use an enum:

public enum Foo {
   INSTANCE;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:

public final class Foo {

    private static final Foo INSTANCE = new Foo();

    private Foo() {
        if (INSTANCE != null) {
            throw new IllegalStateException("Already instantiated");
        }
    }

    public static Foo getInstance() {
        return INSTANCE;
    }
}

Since java5 the best way to do it is to use an enum:

public enum Foo {
   INSTANCE;
}


What is INSTANCE


how to achieve singleton with below 3 lines of code

public enum Foo {
   INSTANCE;
}


any complete workign example link which i can test and see on eclipse.

please advise
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.