Question

class car{
    Salon s ;
}
class Salon{
     Radio musicsystem ;
}
class Radio{
    Button play ;
}
class Button{
     String s ;
}

void main(){
    car mustang = new car( new Salon( new Radio(new Button ("fight club song"))))
}

I can easily imagine there is lots new(new (new(new ....) )). How deep can you go? I "intuitively" feel it can be bad for the compiler/jvm/system to have too many levels of objects...

Does java have restriction on depth?

Was it helpful?

Solution

No, this is no problem at all, especially not at human readable levels like yours.

Let's look at the limits, though. Code like this:

public class Foo {
  public Foo(Foo f) {}
  public static void main(String[] args) {
    new Foo(new Foo(new Foo(new Foo(null))));
  }
}

compiles into:

public static void main(java.lang.String[]);
Code:
   0: new           #2                  // class Foo
   3: dup           
   4: new           #2                  // class Foo
   7: dup           
   8: new           #2                  // class Foo
  11: dup           
  12: new           #2                  // class Foo
  15: dup           
  16: aconst_null   
  17: invokespecial #3                  // Method "<init>":(LFoo;)V
  20: invokespecial #3                  // Method "<init>":(LFoo;)V
  23: invokespecial #3                  // Method "<init>":(LFoo;)V
  26: invokespecial #3                  // Method "<init>":(LFoo;)V
  29: pop           
  30: return     

i.e. for each nesting, it just uses two or more elements on the operand stack: the class to instantiate, the nested object, and any other variables passed along with it.

The maximum size of the operand stack is implementation specific, but any JVM will certainly be able to hold several thousand variables and operate on them with efficiency. For comparison, javac crashes after just 1000 nesting.

So no, your four level deep nesting is absolutely no problem for the JVM.

OTHER TIPS

A satisfactory answer should be, You can go much deeper than you'll ever want to see actually happen in your code.

If you want to entertain the limits just for the fun of it, I believe that the first limit you will in fact hit is the limit on the length of the bytecode of a single method, which is set at a very low 64K.

The only way to truly worry about hitting the limit is when you have recursive constructor calls, such as you might have when constructing an immutable linked list or a similar structure.

The compiler has a limitation of 64 KB for a single method. This is born from the fact that a jump can only go to an absolution byte code position and uses a 16-bit unsigned value. The limitation applies even if you don't have a such a jump.

This has a some surprising consequences. While you might not want to have thousands of nested objects defined this way, you might have a generated class with say thousands of ENUM values. These values are created in one static initialiser method and this also has the same limitation, so you can only have about 3K enum values.

A more likely limitation is large arrays. When you define an array, it actually generates code to set each and every cell. This is pretty inefficient, but general. It also means you can't define an array in Java, with initialised values, with say 10K hard coded values. Again, this is really only a problem for generated code.

Size of your heap memory.When the memory gets full or GC cannot collect objects,it throws java.lang.OutOfMemoryError

So as long as you have memory,it is not an issue

Really, this is no different than writing:

Button button = new Button ("fight club song");
Radio radio = new Radio(button);
Salon salon = new Salon(radio);
car mustang = new car(salon);

The only difference is you're not assigning a variable to each reference, so you can't refer to them later. So, the answer is, as long as you have enough memory to instantiate these objects, you can create as many as you wish.

This isn't really an issue of nesting, or levels deep, as the compiler calls each constructor, initializes the object, then returns a reference to the initialized object. There's no recursion happening here, nor is the stack growing with each level.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top