Thursday, November 20, 2014

Identifiers and limit on Identifier length in Java (maximum length of Class,interface, package, method, variable names).

Fun time.

Though it is not a big thing to consider, yet out of curiosity one can interested to know about the limit on Class names, Interface names, Package names, Variables names, Method names etc.

Before going to start with that let me introduce the term identifier . What is it? The above all names so far we mentioned (Class, Interface, Package, Method, Variable names) are identifiers. Do not confuse with the terms literal and identifier and literal. Definition given by Oracle for Identifier is

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. An identifier cannot have the same spelling (Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal (§3.10.7).

And the rules to remember while writing and identifiers is,

Identifiers must be composed of letters, numbers, the underscore _ and the dollar sign $. Identifiers may only begin with a letter, the underscore or a dollar sign.

class Hummingbird {  
      public static void main(String[] args) {  
           System.out.println("Java Identifiers");  

Considering above piece of code, HummingbirdmainStringargsSystem, outprintln are identifiers. And if we see carefully, from the definition there is no limit on their names. So the length is unlimited. But there is a small and important things to remember here.

  • The language specification have no limit, but JVM may have limit on it.
  • It depends on underlying Operating System as well, since the OS may have restriction on file name where SomeLongestJavaFileName.Java may cross the limit if OS file name length.

And some class names I found personally on internet are (source) (source)

But apart from fun, follow proper naming conventions and don't care how big it is, just name it. That helps lot for the future readers of your code.

No comments:

Post a Comment