3

I understand that in Java all method calls go on a stack. Take the following class for instance:

Class Demo
{
   // Some instance variables
   public Demo()
   {
      initialize();
   }

   public void initialize()
   {
      // Start initialization
      ....

      // Call another method to perform some complex calculation
      int resultVal = helperMethod();

      // Perform the remaining initialization work

   }


   public int helperMethod()
   {
      // Perform some complex calculation 
      ....

      return result;
   }

}

First initialize() (with its state) is pushed onto the stack and then when it calls helperMethod() ,state for helperMethod() is pushed onto the stack too.

But what i want to understand is , is state for Demo() first pushed onto the stack (before even initialize() is pushed) , despite it being a constructor and not a method ?

Are there notable differences between saving constructor state and method state ?

Chiseled
  • 2,280
  • 8
  • 33
  • 59

3 Answers3

2

When it really comes down to it a constructor is just like any other method. It takes parameters of whatever types and returns an object of its own type. It's put on the call stack like anything else, and shows as Demo.<init>()

An exception in your example call stack-trace would look like

Exception in thread "main" java.lang.NullPointerException
    at Demo.helperMethod(Demo.java:30)
    at Demo.initialize(Demo.java:16)
    at Demo.<init>(Demo.java:7)           <---------
    at Demo.main(Demo.java:36)
Stephen P
  • 14,422
  • 2
  • 43
  • 67
2

From a Java language perspective, this is implementation-specific; the JLS doesn't say much about whether methods even require a stack, or what it has to look like, except to say (in 15.12.4.5) that if a method invocation can't happen because the frame can't be created, it should throw a StackOverflowException.

From a Java platform perspective (ie, the language as executed by a compliant JVM), constructors are methods, and thus function in the same way as far as stack frames are concerned. JVMS 2.9 describes constructors as "special methods," but it doesn't change anything about them as far as the stack frame-ness is concerned.

As you may know, a stack overflow exception happens when you invoke too many methods without returning from them; in practice, this is most common when you have infinite recursion. You can cause the same problem with constructors, if each object always constructs another instance of itself (ie, an infinite recursion of construction).

public class ConsBoom {
  public ConsBoom() {
    new ConsBoom();
  }

  public static void main(String[] args) {
    new ConsBoom();
  }
}
yshavit
  • 42,327
  • 7
  • 87
  • 124
  • The stack and the implementation of Constructors as methods are discussed in the JVMS: [here](http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.5.2) and [here](http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.9). – Sotirios Delimanolis Jul 24 '15 at 21:30
  • @SotiriosDelimanolis Yes, which as far as the _JLS_ is concerned, is a description of an implementation. Everything in there is an implementation detail for the Oracle JVM, not a requirement for any implementation of the Java language. – yshavit Jul 24 '15 at 21:32
  • The JVMS, Java Virtual Machine Specification, is a specification for all JVMs. – Sotirios Delimanolis Jul 24 '15 at 21:32
  • I suppose it comes down to whether you consider "Java" to be the language or the platform; I usually consider it as just the language, with the platform called "the Java platform." But I'll amend my answer. – yshavit Jul 24 '15 at 21:35
1

Yes. Calls to constructors use the stack just like regular methods.

Dwayne Towell
  • 8,154
  • 4
  • 36
  • 49