• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursive constructors  RSS feed

 
Saba Shahrukh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code generates error: recursive constructor invocation



while this executes and gives Stack Overflow Error



why?

 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like in the first case you're just in an infinite loop of calling the same constructor, the constructor is never finished so no new objects are created, but in the second case its an infinite loop of creating new objects.
 
Saba Shahrukh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how come in the second case compiler lets me call the constructor recursively.

Why the errors are different while both the code calls the constructor recursively?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first piece of code is simply not allowed by the compiler. You create a new HelloWorld object but in the constructor you're trying to call that same constructor to initialize the object you're creating. The compiler is smart enough to see that that will never work, so it will give you an error.

In the second piece of code, you're creating a different HelloWorld object in the constructor. So, what happens is: you create a HelloWorld, which is initialized by calling the constructor, which then creates another HelloWorld, which is initialized by calling the constructor, which then creates another HelloWorld, which is initialized by calling the constructor, which then creates another HelloWorld, which is initialized by calling the constructor, ... etc. until the stack overflows.

The compiler does a limited analysis on your code, it can detect some problems that would happen when you would run the program, but it does not and cannot analyze all possible runtime problems. In this case, it is able to see that #1 would be a problem, but it doesn't see that #2 would be a problem.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the second case when each object is created memory is used, causing the JVM to run out of you guessed it memory hence the StackOverflowError. But the first case like Tyson said never finish the constructor.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote: . . . The compiler does a limited analysis on your code, . . . but it does not and cannot analyze all possible runtime problems. . . . .
Some things would be impossible to predict at compile‑time. Others would be possible, but predicting them would make compilation take hours for a simple program. There is a limit to what you can expect from a compiler.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its also just that having an infinite loop creating objects isn't necessarily a problem. Maybe I'm just testing my memory? If you are infinitely performing a task, who is the compiler to say that that task isn't worthwhile? But infinitely looking at the same directions for a task doesn't really ever make sense, although theoretically it seems like even that could be allowed.
 
Saba Shahrukh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So its just a limitation of the compiler and also that the second code has a run time error and is out of compiler's scope.. Because there is also possibility that I will call a different constructor to initialise my object..
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its more like a feature of the compiler that it can catch the first case rather than a limitation that it can't catch the second. I ran a quick test and it doesn't matter if there are multiple constructors the compiler will still catch the recursion.
 
Saba Shahrukh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it will catch recursion if we use "this()" but if we call the constructor recursively by creating a new Object then it is not able to detect and hence Stack Overload.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saba Shahrukh wrote:Yes it will catch recursion if we use "this()" but if we call the constructor recursively by creating a new Object then it is not able to detect and hence Stack Overload.


I don't think "limitation" or "not able to detect" is the correct terms here. That is how it is defined in the Java Language Specification. The compiler is doing what it is designed to do. The phase "not able to detect" implies that the compiler tries and fails, instead of doing it that way because it is supposed to.

Henry
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The construct this(); is supposed to invoke a different constructor, not the same one.
 
Saba Shahrukh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"not able to detect" implies that the compiler tries and fails

Understood
Thanks
 
Saba Shahrukh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the concept of recursion important for OCJP cerification Exam.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!