• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How recursion happens in this code?

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code throws StackOverflowError at runtime. Emp mgr = new Emp(); is caused for recursion. But I can’t understand that how recursion happens when we call e = new Emp(); in main method. I try to debug this in Netbeans but it doesn’t help.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's really not that hard You'll definitely be ale to spot the actual issue after a (few) pointer(s).

What happens when this statement is executed?
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:It's really not that hard You'll definitely be ale to spot the actual issue after a (few) pointer(s).

What happens when this statement is executed?


Default constructor of Emp is called.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:Default constructor of Emp is called.

True! But is this really the only thing happening?
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Mushfiq Mammadov wrote:Default constructor of Emp is called.

True! But is this really the only thing happening?


Not of course, sorry, at first instance variable initialize then default constructor is called. But instance variable mgr also call default constructor. It is unclear that I think default constructor is called once here, but I know I think wrong in this point, maybe recursion happens here, but I can't find it exactly
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:
Roel De Nijs wrote:
Mushfiq Mammadov wrote:Default constructor of Emp is called.

True! But is this really the only thing happening?


Not of course, sorry, at first instance variable initialize then default constructor is called. But instance variable mgr also call default constructor.

Exactly! So for every Emp instance being created, the default constructor is called and the instance variable mgr is initialized. Which code is used to initialize mgr?
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote: Which code is used to initialize mgr?


This new Emp();


I am ashamed I can't still catch after these pointers
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that the following code is recursion:


I try to relate the above code with this code but I can't find any correspondence.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:This new Emp();

True! So you create a new Emp instance again. And what does happen when you create a new Emp instance?
 
Paweł Baczyński
Bartender
Posts: 1876
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to realize one important thing.
Fields of an object are initialized before constructors are called.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:I know that the following code is recursion:

True!

Mushfiq Mammadov wrote:I try to relate the above code with this code but I can't find any correspondence.

If you don't find the correspondence based on the last tip, I'll give another hint based on that recursive code snippet
 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:
Fields of an object are initialized before constructors are called.


Fields of class are not initialized before constructor call but Constructor initializes the fields of class.
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Mushfiq Mammadov wrote:This new Emp();

True! So you create a new Emp instance again. And what does happen when you create a new Emp instance?

Evrika Probably I can understand! The default constructor is called and the instance variable mgr is initialized. When mgr is initialized this process is repeated and recursion happens Am I correct?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:Evrika Probably I can understand! The default constructor is called and the instance variable mgr is initialized. When mgr is initialized this process is repeated and recursion happens Am I correct?

Exactly! When you create an Emp instance, the instance variable mgr is initialized. So a new Emp instance is created. So again the instance variable mgr is initialized and thus a new Emp instance is created. And the instance variable mgr is initialized once more, so another new Emp instance is created. And so on...

This codeis equivalent toThat's almost exactly the same code as the code you posted which had recursion
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote: This codeis equivalent to

That is it which I need. If you write this code at first post everything would be clear. But you gave a chance to me that I found it myself and it was very helpful and enjoyable for me. Thanks a lot, Roel
 
Paweł Baczyński
Bartender
Posts: 1876
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gajendra Kangokar wrote:Fields of class are not initialized before constructor call but Constructor initializes the fields of class.

Technically, you are right.
What I meant was:
Fields of an object are initialized before any instruction in the constructor other than this() or super() is called.

So this prints:
Variable a
Variable b
Constructor


JLS wrote:Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

2. If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.

3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.

5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gajendra Kangokar wrote:
Paweł Baczyński wrote:
Fields of an object are initialized before constructors are called.


Fields of class are not initialized before constructor call but Constructor initializes the fields of class.

True!

If you compile this codeyou'll get this code
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Gajendra Kangokar wrote:
Paweł Baczyński wrote:
Fields of an object are initialized before constructors are called.


Fields of class are not initialized before constructor call but Constructor initializes the fields of class.

True!

If you compile this codeyou'll get this code


Fields of class are not initialized before constructor call but Constructor initializes the fields of class.

It's more subtle than that. mgr is initialized to null before the constructor runs. It's "initialized" after the constructor in the sample only because it's initialized by (given the return value of) the constructor.
The order is 1, Static 2. Instance 3. Constructor

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:That is it which I need. If you write this code at first post everything would be clear. But you gave a chance to me that I found it myself and it was very helpful and enjoyable for me. Thanks a lot, Roel

That was my intention from the beginning. I didn't want to spoil your fun of discovering the cause of the StackOverflowError yourself. Glad to hear you liked this approach!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic