Given the following program:
class A {
static int java = 42;
static int System = -1;
public static void main(String[] args) {
java.lang.System.out.println("Foo");
}
}
Compiling this gives me an error saying that “int cannot be dereferenced”. The reason is obvious, but how should one handle situations like this. It is especially relevant in code-generation scenarios, where one cannot know what user code is intertwined with generated code.
In C# I would just use the “global::” namespace specifier before “java.lang”, but what do you do in Java?
There is no general solution for this type of naming clash in Java. It may be upsetting to find out, but it very rarely comes up as an actual problem in practice. There are a few identifiers to avoid as member variable names, like
javaandcom, because they are used for top-level domain names.There are some other potential clashes as well, which we avoid by adhering to the naming conventions — start class names with uppercase and variable names with lowercase.