The same enumeration and variable names

I have the following code (I don't know it very well;)):

public class Clazz1 { public int test = 10; public enum test {a, s, d, f } void sth() { // ... } } 

Is there any way to access this listing? When I type 'test', it always means int variable. What are the rules associated with this situation - why even a compiler allows you to have an enumeration and int with the same name?

+6
source share
4 answers
 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 is this done

 $ 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 types of classes, and using the enum keyword causes the 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())); } } 

Now you can use the ec and Enum class methods to get objects. The fact is that you REALLY do not want to do this. Because it is equivalent:

 class Foo { class Bar { } int Bar; } 

What will make javak spit unnecessary epitaphs.

For more information on Enum reflection classes, check out the java api documentation

+4
source

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

Update: no, this does not work (see comments), so you need to resort to such dirty tricks:

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

But there are reasons why there are Java variable name conventions : to make such situations less likely, and Java code more readable:

  • Enum names (like other class and interface names) must be in UpperCamelCase
  • Local variable names and field names must be in lowerCamelCase Constants
  • (static and trailing fields) must be in UPPER_UNDERSCORE_CASE.
+3
source

Variable and type names are in different namespaces:

 String String = "Hello!"; 

Not sure what you mean by "this always means int variable". Edit A, I understand what you mean; Sean answers how to determine the name.

+1
source

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 parts first fall.

I would really suggest:

  • Distinguish different variables with different names

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

IMHO :-)

PS: The answer is "namespaces." You can find it in the Java language specification :-)

+1
source

Source: https://habr.com/ru/post/899928/


All Articles