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.

Ads by Google

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<Content> classList = new ArrayList<Content>();
		List<Content> methodList = new ArrayList<Content>();
		List<Content> attributeList = new ArrayList<Content>();

		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<Content> classList,
			List<Content> methodList, List<Content> attributeList)
			throws Exception {
		Enumeration<JarEntry> 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<Content> methodList) {
		Method[] methods = clazz.getMethods();
		for (Method method : methods) {
			methodList
					.add(new Content(method.getName(), clazz.getSimpleName()));
		}
	}

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

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

	private static void print(List<Content> 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.

previous post: Post to Twitter using Java

next post: System.gc() Invocation – A Suitable Scenario

12 comments on “Longest Class, Method and Attribute Names in Java

  1. 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. 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?

    • No!! There is no such restriction. Is the content misleading?

  3. 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.

    • Yes Praveen, thats right. Presently, I am working menu redesign and I will fix this also along with that.

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

  5. Hi Joe

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

    Regards
    Jinu

  6. 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

  7. mam cant run this file why,if so getting error

  8. why this is working in jar file only,

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>