1

i know this is trivial question but this is totally intriguing me;

As inner classes' (non-static) members cannot be directly accessed without instance of it even in outer class,

and while to access static constants u have to scope with outer class hai.x ; but in the case of "use of enums switch as case constants" it seems to be other way around; look dostuff2()

case RED: works but case value.RED gives error an enum switch case label must be the unqualified name of an enumeration constant

i know above statement says it all;i'm just curious

if we assume that compiler pads that 'value. ' to all switch constants.. why this isn't the case with normal declartion of enum as well value v=RED

    public class enumvalues
{
    public enum values
    {
        RED,GREEN,VALUE      
    }
    public class hai
    {
        static final int x=90;                    
    }
    public static class statichai
    {

    }
    public static void main(String []args)
{

}

    public  void dostuff2(values v)
    {
       //  v =RED this is illegal 

       // System.out.println(RED);  this will not compile because it cannot be accessed directly
      // System.out.println(x); this will not compile because it cannot be accessed directly



        switch(v)
        {
            case RED: //use of value.RED is strictly forbidden
            System.out.println("red");
        }
    }
}
manifold
  • 437
  • 6
  • 23
  • Did you try `value.RED` or `values.RED`? (notice `s` in `values` in the latter) – Abubakkar Jun 21 '16 at 10:29
  • enum values must normally be qualified. Only for switch cases a short notation was made available. _That's all._ – Joop Eggen Jun 21 '16 at 10:32
  • That's just a rule of the Java programming language. There's no clear answer to the question "why". Because the designers of the Java language thought this was a good idea. It's already clear what type of enum you're switching on, so having `values.RED` instead of just `RED` in the `case` would only be unnecessary verbosity. – Jesper Jun 21 '16 at 10:32
  • @Jesper shouldn't it be the same with value v =RED as well ,see my question completely – manifold Jun 21 '16 at 10:33
  • Nested enums are always static, see http://stackoverflow.com/questions/663834/in-java-are-enum-types-inside-a-class-static – Jiri Tousek Jun 21 '16 at 10:34
  • This is a valid question, no need to vote it down. It might be just a rule of the Java programming language, but that doesn't mean it makes sense to a new user. – Dawied Oct 18 '17 at 13:48

1 Answers1

2

Because it would require to create special case for enums for check. consider another example

interface SomeInterface{}
enum Values implements SomeInterface {RED};
enum MoreValues implements SomeInterface {RED};

public  void dostuff2(SomeInterface value)
{
value = RED;
}

It is quite similar to your code to yours, and dostuff2 is exactly same. As under hood for compiler enums are just regular classes. So to handle your example, you needed to add special case to handle enums.

user902383
  • 8,420
  • 8
  • 43
  • 63