-1

I have a singleton class as shown below:

public class GlobalClass
{
    public final int _SUCCESS = 0;    
    private static GlobalClass singleton = new GlobalClass( );

    private GlobalClass(){} //Private constructor for Singleton class.

    public static GlobalClass getInstance()
    {
        return singleton;
    }
}

I want to create a enum variables. I am not able to do it. I wrote the below code.

public class GlobalClass
{
    enum myEnum {
        _ZERO, _ONE, _TWO
    }
    public final int _SUCCESS = 0;
    //public final int _ONE = 1;

    private static GlobalClass singleton = new GlobalClass( );    
    private GlobalClass(){} //Private constructor for Singleton class.

    public static GlobalClass getInstance()
    {
        return singleton;
    }    
}

What is wrong with the above?

ArK
  • 20,698
  • 67
  • 109
  • 136
pkthapa
  • 1,029
  • 1
  • 17
  • 27
  • Why are you "not able to do it"? What happens? Does the compiler throw an error? Could you provide a *little* bit more information? – QBrute Nov 16 '16 at 07:27
  • 2
    Well ... You obviously declared an enum. So what does "I am not able to do it" exactly mean? – Seelenvirtuose Nov 16 '16 at 07:28

1 Answers1

0

You only declared the enum (which is a special form of a class) inside your GlobalClass.

What you are missing is the declaration of the variable with that type as

private myEnum _MY_ENUM_INSTANCE;

In addition I would recommend that you implement your singleton pattern in a more efficient way as described in What is an efficient way to implement a singleton pattern in Java? (that is also using an enum for some non-obvious reasons.)

Community
  • 1
  • 1
cyberbrain
  • 3,433
  • 1
  • 12
  • 22