views:

227

answers:

5

A very poor style to code but sometimes unavoidable. It is an extreme example. So

  1. is there some limit for nesting classes?
  2. are they equivalent?
  3. how do you deal with such situations? Create library?

Code

new FileObject().new Format().new Words().new Some().new Continue someThing;

((((new FileObject()).new Format()).new Words()).new Some()).new Continue someThing;
+2  A: 

The example you give are non-static nested classes. (You need an object of the type of the enclosing class to instantiate the nested class.) Nested non-static classes are seldom used. Usually you can do just as well with static nested classes, or non-nested classes with default (package) scope.

aioobe
+1  A: 

At the bytecode level, there is no such thing as a nested class - the compiler turns them into separate top-level classes that can access the parent class's methods via synthetic accessor methods.

There is no explicit limit on the nesting depth. The most relevant implicit limit is the name of the generated .class file, which replicates the containment hierarchy, i.e. FileObject$Format$Words$Some$Continue.class. This will eventually run into filename length limits, depending on the filesystem.

Michael Borgwardt
Good answer but I think the accessor methods only get created when the methods in the nested class aren't package-private.
Helper Method
+1  A: 

As state in Inner Classes and Enclosing Instances (in langage spec), the nested class is a sort of static inner class.

So I suppose in your case it's inner, not nested.

In inner case I do some factory instance method in enclosing class, because I don't love the expression new Toto().new Foo()... you obtain a foo object, but where is the toto object ?

For nested, I do new Toto.Foo.Bar.Other.And.So.On().

Istao
+3  A: 

The Sun JVM has a limit of 65536 bytes for a fully qualified class name encoded as a string using modified UTF-8 as described in "The class File Format" spec. It comes from 'u2' capacity (16 bits) -- the type of 'length' from constant pool 'CONSTANT_Utf8_info' structure used to store class names in class files.

From maximum length for a java class name

Also, the jar format (based on zip) has a maximum file length also of 64k. (Wikipedia, ZIP, File headers.)

So, the limit for class nesting is when the name reaches 64k, but I think your sanity might hit its limit long before that!

mdma
+1  A: 

A very poor style to code but sometimes unavoidable.

The second part of that statement is patently false. It is always possible to avoid using nested classes at the implementation level.

I can think of no sensible design reason to nest named classes more than one level, and anonymous classes more than 3 or 4 levels. (I do remember one time using 3 or 4 levels of anonymous inner classes in an Eclipse RCP application with really complicated callbacks. But that was really unusual, and in retrospect it was a bad idea.)

1) is there some limit for nesting classes?

In theory the limit is the 64K limit on fully qualified classnames. In practice, the limit is common sense.

2) are they equivalent?

They? If you are asking if nested classes are equivalent to non-nested classes, the answer is Yes ... to all practical intents. The only thing that you can do with nested classes that you cannot do with regular classes is share things that are declared as private. And the workaround is to relax your access to package private.

3) how do you deal with such situations? Create library?

I don't know what situation you are talking about. The normal way to modularize a Java programs consisting of multiple classes is to use Java packages rather than class nesting. Is that what you are missing?

Stephen C