你可以在java枚举中的成员数量的限制是什么? [英] What's the limit to the number of members you can have in a java enum?

查看:634
本文介绍了你可以在java枚举中的成员数量的限制是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你在这样的java中有一个假设的枚举(纯粹用于演示目的,这不是我真正期望使用的代码):

Assuming you have a hypothetical enum in java like this (purely for demonstration purposes, this isn't code i'm seriously expecting to use):

enum Example{
    FIRST,
    SECOND,
    THIRD,
    ...
    LAST;
}

编译器停止之前,该枚举中可以包含的最大成员数

What's the maximum number of members you could have inside that enum before the compiler stops you?

其次,在代码引用枚举时,在运行时是否有任何性能差异,比如说10个成员,而不是100个或1000个存储大量类所需的内存开销)?

Secondly, is there any performance difference at runtime when your code is referencing an enum with say, 10 members as opposed to 100 or 1,000 (other than just the obvious memory overhead required to store the large class)?

推荐答案

找到这类问题的答案的最好方法是尝试。从一个Python脚本开始生成Java文件:

The best way to find out the answer to this type of question is to try it. Start with a little Python script to generate the Java files:

n = input()
print "class A{public static void main(String[] a){}enum B{"
print ','.join("C%d" % x for x in range(n))
print '}}'

现在尝试1,10,100,1000 ...工作正常,然后BAM:

Now try with 1,10,100,1000... works fine, then BAM:

A.java:2:代码太大
C0,C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12 ,C13,C14,C15,C16,C17,C18,C19,C20,C21,C22,...

A.java:2: code too large C0,C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13,C14,C15,C16,C17,C18,C19,C20,C21,C22,...

似乎我遇到某种内部限制。不确定是否是一个文档限制,如果它取决于特定版本的编译器,或者如果它的一些系统相关的限制。但对我来说,限制在3000左右,似乎与源代码大小相关。也许你可以编写自己的编译器来绕过这个限制。

Seems like I hit some sort of internal limit. Not sure if it's a documented limit, if it's dependent on the specific version of my compiler, or if its some system dependant limit. But for me the limit was around 3000 and appears to be related to the source code size. Maybe you could write your own compiler to bypass this limit.

这篇关于你可以在java枚举中的成员数量的限制是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆