How many times can classes be nested within a class?

前端 未结 6 1332
时光说笑
时光说笑 2020-12-31 13:49

I came across this questions on one of the online Java Tests. The options were 4,5,8 and any number of times.

I have only used one inner class, but have never tried

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-31 14:10

    The true answer is none of the above. Javac will of course allow arbitrary nesting, but the underlying file system and/or OS have restrictions. We found recently that during startup the JVM running a Glassfish Java EE application server instance will hold open files to each .class during loading/JIT compilation. If you're running CentOS Linux say, the default limit on open files/process is 1024, so if you have a lot of classes being loaded at once and don't set the ulimit -n to a higher value, the JVM will hit "too many open files". And @Mark Byers' example shows that the .class file names get very long and can hit the file system name length limit, if there is one.

提交回复
热议问题