mmatloka
mmatloka

Reputation: 2014

Same names of enum and variable

I've got following code (I know its not nice ;) ):

public class Clazz1 {

    public int test = 10;

    public enum test {a, s, d, f  }

    void sth() {
        // ...  
        }
}

Is there any way to acces this enum? When I type 'test' it always means that int variable. What are rules connected with this situation - why even compiler allows to have enum and int with the same names?

Upvotes: 6

Views: 3853

Answers (4)

Ahmed Masud
Ahmed Masud

Reputation: 22402

public class Clazz1 {

    public int test = 10;

    public enum test {a, s, d, f };

    public static void main() {
        System.out.println("a: " + Clazz1.test.a);
    }
}

When this is run

$ javac Clazz1.java 
Clazz1.java:8: error: non-static variable test cannot be referenced from a static context
    System.out.println("a: " + Clazz1.test.a);
                                     ^
Clazz1.java:8: error: int cannot be dereferenced
    System.out.println("a: " + Clazz1.test.a);
                                          ^

Enumerations are actually special class types and the use of enum keyword causes class to be created. For instance:

import java.util.Arrays;
import static java.lang.System.out;

public class Clazz1 {

    public enum test
    {
    a, b, c, d, e, f;
    };

    public static int test = 10;


    public static void main(String[] args) {
    Class<?> ec = Clazz1.test.class;
    out.format("Name: %s, constants: %s%n\n", ec.getName(), 
        Arrays.asList(ec.getEnumConstants()));
    }
}

You can now use ec and Enum class methods to get the entities. The thing is that you REALLY don't want to do this. Because it's equivalent of:

class Foo {
       class Bar { }
       int Bar;
}

Which will make javac spit unneeded epitaphs.

For more information on Enum reflection classes check java api documentation

Upvotes: 4

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299048

Accessing static members of the test enum should still work, and the enum constants are static members. Try Clazz1.test.a.

Update: no, it doesn't work (see comments), so you'll need to resort to dirty tricks like this:

// this works but is awful not compile-time-safe
test a = Clazz1.test.class.getEnumConstants()[0];  

But there are reasons why the Java variable naming conventions exist: to make situations like this less likely, and Java code more readable:

  • Enum names (as other class and interface names) should be in UpperCamelCase
  • local variable names and field names should be in lowerCamelCase
  • constants (static and final fields) should be in UPPER_UNDERSCORE_CASE.

Upvotes: 3

Dave Newton
Dave Newton

Reputation: 160261

Variable and type names are in different namespaces:

String String = "Hello!";

Not sure what you mean by "it always means that int variable". Edit Ah, I see what you mean; Sean's answer addresses how to scope the name.

Upvotes: 1

paulsm4
paulsm4

Reputation: 121799

This example is really a bit like driving a car into a wall, and trying to figure out how the engine works by observing which pieces fall part first.

I'd really suggest:

  • Distinguish different variables with different names

  • Drive your car on the road; don't slam it into walls

IMHO :-)

PS: The answer is "namespaces". You can look it up in the Java language spec :-)

Upvotes: 1

Related Questions