Longest Class, Method and Attribute Names in Java

Just got curious to see what is the longest class name in Java JDK bundle and extended that curiosity to method and attribute names as well.

Wrote a tiny Java program to load all classes, their methods and attributes from a Jar to print their name to a file. Then ran that on JDK 1.6 (rt.jar) and got the following results.

  • For class name the award goes to (92 chars) InternalFrameInternalFrameTitlePaneInternalFrameTitlePane
    MaximizeButtonWindowNotFocusedState
  • For method name the award goes to (66 chars) localizableRUNTIME_MODELER_PORTNAME_SERVICENAME
    _NAMESPACE_MISMATCH
    belonging to class ModelerMessages.
  • For attribute name the award goes to (60 chars) ER_INVALID_NAMESPACE_URI_VALUE_FOR_RESULT_PREFIX
    _FOR_DEFAULT
    belonging to class XSLTErrorResources.

long

Hope you are following naming conventions strictly in your projects. I welcome you to run this below program on your project or your favorite packages like Spring / Hibernate etc and share your interesting findings in the comments.

This Java program will print all the class, method and attribute names from the Jar file, sorted in descending order of length.

package com.javapapers.java;

import java.io.File;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class LongNames {
	public static void main(String[] args) throws Exception {
		String javaFile = "C:\\Program Files\\Java\\jdk1.6.0_24\\jre\\lib\\rt.jar";

		List classList = new ArrayList();
		List methodList = new ArrayList();
		List attributeList = new ArrayList();

		System.out.println("Processing...");
		getList(new File(javaFile), classList, methodList, attributeList);

		sort(classList);
		print(classList, new PrintWriter("ClassList.txt"));
		System.out.println("Class list complete.");

		sort(methodList);
		print(methodList, new PrintWriter("MethodList.txt"));
		System.out.println("Method list complete.");

		sort(attributeList);
		print(attributeList, new PrintWriter("AttributeList.txt"));
		System.out.println("Attribute list complete.");
	}

	private static void getList(File file, List classList,
			List methodList, List attributeList)
			throws Exception {
		Enumeration jarEntries = new JarFile(file).entries();

		while (jarEntries.hasMoreElements()) {
			JarEntry jarFile = jarEntries.nextElement();
			String className = jarFile.getName();
			if (className.endsWith(".class")) {
				className = className.replaceAll("\\.class", "");
				className = className.replaceAll("/", ".");
				try {
					if (className != null && !className.isEmpty()) {
						Class clazz = Class.forName(className, false,
								ClassLoader.getSystemClassLoader());

						// inner class hack
						if (clazz.getSimpleName().length() > 0) {
							classList
									.add(new Content(clazz.getSimpleName(), ""));
							getMethodList(clazz, methodList);
							getAttributeList(clazz, attributeList);
						}
					}
				} catch (Exception e) {
					System.out.println(e);
				}
			}
		}
	}

	private static void getMethodList(Class clazz, List methodList) {
		Method[] methods = clazz.getMethods();
		for (Method method : methods) {
			methodList
					.add(new Content(method.getName(), clazz.getSimpleName()));
		}
	}

	private static void getAttributeList(Class clazz,
			List attributeList) {
		Field[] fields = clazz.getFields();
		for (Field field : fields) {
			attributeList.add(new Content(field.getName(), clazz
					.getSimpleName()));
		}
	}

	private static void sort(List nameList) {
		Collections.sort(nameList, new Comparator() {
			public int compare(Content c1, Content c2) {
				//descending
				return c2.content.length() - c1.content.length();
			}
		});
	}

	private static void print(List list, PrintWriter file) {
		for (Content str : list) {
			file.println(str.content.length() + "\t" + str.content + "\t"
					+ str.className);
		}
		file.close();
	}

}

class Content {
	public Content(String content, String className) {
		this.content = content;
		this.className = className;
	}

	String content;
	String className;
}

This Core Java tutorial was added on 18/12/2013.

Comments on "Longest Class, Method and Attribute Names in Java" Tutorial:

  1. Nagendra says:

    According to the naming conventions, its good to follow this.

    But my doubt is why they have restricted to this specific length?

    Is there any reason behind this?

    Thanks,
    Nagendra.

  2. Asim says:

    92 chars for class name,it is restricted,we can not go beyond to it.if this is restricted how did you calculate it?Is class name length dependent on JDK version?

  3. Praveen says:

    One suggestion, your site template has very limited space for text/code. This makes difficult to scroll sidewise to understand code.

    I appreciate your posts and interest.

  4. Anu says:

    I executed the above program.Works perfect..Good one Joe..

  5. Divya says:

    Nice Tutorials are here…..

  6. Jinu says:

    Hi Joe

    I tried to find in javadocs this class InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState. I see none. Why? What is the use of this class?

    Regards
    Jinu

  7. Pratik says:

    It worked but got different results , I made some changes I used rt.jar which is in jre7 library as I don’t have java installed.
    Thanks

  8. chitra says:

    mam cant run this file why,if so getting error

  9. Anonymous says:

    why this is working in jar file only,

Your Comment

↑ Go to top