Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why is this giving me a StackOverflowError?

 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is this giving me a StackOverflowError?object references c1 and c2 are created separately .is there a infinite recursion...
 
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
Ramya Subraamanian wrote:why is this giving me a StackOverflowError?object references c1 and c2 are created separately .is there a infinite recursion...

Let's see if you can figure this one out yourself with some guidance.

If you run this application, the main method is executed. So a new Calf instance is created. What happens when an instance is created?
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once an instance is created, it is put into the object heap. main methods is static , so c2 is created before c1.
 
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
But what happens exactly if this statement is executed?
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after object is put in heap, default constructor is called, which calls the super constructor of object.
 
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
Ramya Subraamanian wrote:default constructor is called, which calls the super constructor of object.

True! But is this really the only thing happening?
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static initializers and instance initializers of the super class and class are run before the constructors . memory is allocated for the instance variables in the class and also of its super class. so now c1 is created and allocated memory.
Stack is created for each individual thread, so a stack is created for main.
 
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
Ramya Subraamanian wrote:static initializers and instance initializers of the super class and class are run before the constructors . memory is allocated for the instance variables in the class and also of its super class. so now c1 is created and allocated memory.

Exactly! So for every Calf instance being created, the default constructor is called and the instance variable c1 is initialized. Which code is used to initialize c1?
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while default constructor is already called by c2, keyword new is used to initialize c1 which calls the default constructor again.calls from c1 and c2 makes a infinite recursive loop of the constructor causing stackoverflow error ...
 
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
Ramya Subraamanian wrote:while default constructor is already called by c2, keyword new is used to initialize c1 which calls the default constructor again.calls from c1 and c2 makes a infinite recursive loop of the constructor causing stackoverflow error ...

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

In this topic you'll find the same question about a similar code snippet. And you'll also notice that I asked very similar questions in both topics

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay that's cool, so I am not the only one getting weird doubts
This code runs fine, if i call a static variable . Is it because of permGenSpace in heap for static variables. but then again main is also static.
 
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
Ramya Subraamanian wrote:This code runs fine, if i call a static variable . Is it because of permGenSpace in heap for static variables. but then again main is also static.

This code runs indeed fine and prints "main method" at runtime (and no StackOverflowError is thrown at runtime). The reason for this behavior is very simple and completely unrelated with permGenSpace or main method being static.

In this code snippet c1 is a class (static) variable. What do you know about class (static) variables? When are these variables initialized?
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Let's see if you can figure this one out yourself with some guidance.
I really adore your way of making OP understand their problem and solve them self with some guidance.
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this code snippet c1 is a class (static) variable. What do you know about class (static) variables? When are these variables initialized?

Sequentially static variables are initialized first, then static blocks , then main method called, then the rest like in the code below. and hence runs without an issue.

Output is :
static variable initialized
static instance initializer
Object in main method initialized.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:Sequentially static variables are initialized first, then static blocks

Indeed! And more importantly: static variables are initialized only once (not at each creation of an instance, like with instance variables). And initializer blocks are executed only once as well (in contrast to instance initializer blocks which are executed at each creation of an instance).

Ramya Subraamanian wrote:

A "static instance initializer" does not exist! It's either static initializer block or an instance initializer block. The combination is impossible!

I am also a bit surprised to see such advanced (complex) code in your code snippetYou are using an instance initializer block in an anonymous inner class. The latter is definitely not on the OCA exam (but on the OCP exam). This code snippet will produce the same output, but looks much easier (using only topics covered on the OCA exam)Output:
static variable initialized
static initializer block
Object in main method initialized.


Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic