Naming classes that might collide with java.lang. *
Since classes are java.lang.*
implicitly imported into every source file, it makes it difficult for the developer to use these class names in their own namespaces. For example, someone might want to create a class Compiler
since it is a generic name and java.lang.Compiler
is rarely used. Thus, the developer will have to use his full name, for example com.my.organization.my.very.long.package.name.Compiler
, every time, which does not improve the readability of the code. Importing this class explicitly would be very confusing and changing the name to something more specific is not always an option, becauseCompiler
may instead be an interface for a generic compiler to do nothing to anything. Thus, in some cases, the only option is to add a prefix or suffix to distinguish the name. Another example I can imagine is a forum app which can have classes like Author
, Post
and yes is Thread
. Adding something to would Thread
clearly distinguish it from functionally similar classes. Are there any class naming conventions in such cases? The least intrusive option I personally see is to add an underscore, for example Thread_
.
In general, I could come up with names that avoided being so general that they contradicted each other, but even in situations where you cannot, if your code is in a package, the unqualified name would be the package class, not java.lang
one:
stuff/Compiler.java
:
package stuff;
public class Compiler {
public static long value = 42L;
}
stuff/Foo.java
:
package stuff;
public class Foo {
public static void main(String[] args) {
System.out.println(Compiler.value); // <== Compiler is stuff.Compiler
}
}
Running this ( java stuff/Foo
) prints 42
.
source to share