In one article came across the following lines:

The elements of enum Season (WINTER, SPRING, etc.) are statically available instances of the Season enum class. Their static accessibility allows us to perform comparison using the reference comparison operator ==.

And how does static accessibility allow you to perform comparisons using the reference comparison operator == ? Why non-static objects cannot be compared using == , but static ones can be?

    1 answer 1

    When comparing with == , the pointers are compared, and the static fields of the class are the same for all instances of the class — the pointers to them are determined at the moment the program is started. If the field is final, then it does not change throughout the program. And the enum elements are just the final ones (in your link, later in the text, an example of decompiling enum is given - this is a class with public static final fields).

    Why non-static objects cannot be compared using ==, but static ones can be?

    Immutability of static field pointers allows you to compare them

    The following class can serve as an exemplary enum analogue (“approximate”, because for completeness of the match, you must also implement the valueOf , values methods):

     public class Season { public static final Season WINTER = new Season(); public static final Season SPRING = new Season(); public static final Season SUMMER = new Season(); public static final Season AUTUMN = new Season(); private Season() {} } Season a = Season.AUTUMN; Season b = Season.AUTUMN; System.out.println(a == b); //true