0

As a beginner in Java I was introduced to enums and that just got me really confused with type declaration. I would like to share what I know, so that one of you might find a problem in the way I think.

I have learned that there are two datatypes, primitive datatypes and object datatypes. When declaring a variable, you first write the datatype and then the identifier of the variable.

When I declared a datatype int variable, I have the following idea in my head:

int a = 1; int as datatype references to the primitive integer datatype.

When I declared a datatype Integer variable, I have the following idea in my head:

Integer b =  1;  Integer as datatype references to the object datatype (that in this case is an instance of the class integer)

When I declare an enumerator variable, I have the following idea in my head:

enum C {EXAMPLE1, EXAMPLE2}; No clue what enum references to.. Is it an object datatype and an instance of class enum? If so, where is the capital letter E in enum.

So here the problem starts. I have learned that enum is not a primitive datatype, so the only posibility is that it is an object. But I have also learned that declaring an object of a class as datatype means that the identifier should start with a capital letter like Integer, Boolean etc. I can however declare an enum without a capital letter. Does this mean it is not an object? Or is an enumerator just a keyword like class and not a variable datatype like I assumed it to be.

If enum is not a variable datatype but rather a keyword like class then I wonder if the following mindset is correct:

enum Test {EXAMPLE1, EXAMPLE2}
Test e = Test.EXAMPLE; Does Test reference to the object datatype? (That 
in this case is an instance of the enumerator Test) 

If Test is an instance of the enumerator Test, how is this possible? I only thought it was possible to create objects from classes but I have also read something about constructors in enumerators.

I have been looking for answers on this question for a couple of hours now with no success. I hope I haven't messed up your head by now. But I truely hope that there is someone that can make some sense of this and actually help me out.

everton
  • 7,579
  • 2
  • 29
  • 42
  • 1
    Got to ask: what does this have to do with C#? – ProgrammingLlama Sep 08 '18 at 02:33
  • 2
    By the way the capitalization is just a convention, it doesn't actually do anything. – harold Sep 08 '18 at 02:38
  • I am not very common with C# but I have heard often that C# and Java are related in syntax. One that understands C# would probably understand my question as well and help me out. –  Sep 08 '18 at 02:51
  • 1
    For the Java/C# thing, unfortunately enums is one of the few areas where they are very different. – harold Sep 08 '18 at 03:01
  • In Java enums are objects with a backing integer known as the ordinal. In C# an enum is basically a named int (it can actually be any primitive data type). C# enums are more analogous to that of C/C++, except that their names are stored. – vandench Sep 08 '18 at 03:42

2 Answers2

1

Welcome to StackOverflow!

You can think of an enum as a special java type for declaring a "group of constants". Simple example:

enum Language {
    PORTUGUESE,
    ENGLISH
}

Usage:

Language yourLanguage = Language.ENGLISH;

if (language == Language.ENGLISH) {
    System.out.println("Hi!");
}

To this extent, you can think of Language as the enum type, and PORTUGUESE and ENGLISH as constants from this type. When you eventually start to explore enum's capabilities beyond just defining these constants by names, it becomes clearer that these constants are indeed objects, with fields and methods. Example:

enum Language {
    PORTUGUESE("Olá!"), // Call to constructor initializing PORTUGUESE instance
    ENGLISH("Hi!");   // Call to constructor initializing ENGLISH instance

    private final String greeting; // Instance field

    // Constructor!
    Language(String greeting) {
        this.greeting = greeting;
    }

    public void greet() {
        System.out.println(greeting);
    }
}

Usage:

Language yourLang = Language.ENGLISH;
yourLang.greet();
everton
  • 7,579
  • 2
  • 29
  • 42
1

I can however declare an enum without a capital letter. Does this mean it is not an object?

No.

Or is an enumerator just a keyword like class and not a variable datatype like I assumed it to be.

enum, like class, is a keyword -- see JLS section 8.9.

An enum declaration specifies a new enum type, a special kind of class type.

enum constants are instances of the enum type, which in turn extends the Enum superclass.

enum Test {EXAMPLE1, EXAMPLE2}

So here Test defines an enum type. EXAMPLE1 and EXAMPLE2 are enum constants, which are instances of the Test enum type. No other instances can be created at runtime, but you can create other references to those instances.

Test e = Test.EXAMPLE

So here, the local variable e points to the enum constant; e and Test.EXAMPLE have the same address in memory, so to speak.

Test.class is a construction that makes sense - it gives you access to the class instance defined by the enum declaration. Test.EXAMPLE.class doesn't make sense, nor does e.class, because those are instances. Test.EXAMPLE.getClass() and e.getClass()

It's very much like java.lang.Boolean

Boolean b = Boolean.TRUE

but not exactly

Boolean b = new Boolean(true); // OK
Test    t = new Test(); // NOT ALLOWED
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
  • Thank you so much ;) After seeing how the compiler handles the enum's and you mentioning that the constants are instances it all made sense to me. –  Sep 08 '18 at 12:12