• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StackOverFlowError help. Doing research but

 
Greenhorn
Posts: 26
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
I was tinkering with some code and I made an interesting discovery by accident. The compiler would display a StackOverflowError if I did the following:
a) create an instance object of a class that is assigned to a reference variable [Line 5]
b) declare a constructor that does something.(In this case, it calls a method) [Line 7]
c) attempt to run that same constructor from main() [Line 19]

Here is the original code below. (I am using Eclipse IDE by the way. That is why I used a package if you were wondering.)

The console will display something like this.
Exception in thread "main" java.lang.StackOverflowError
at stackoverflow.foo.Foo.<init>(Foo.java:7)
at stackoverflow.foo.Foo.<init>(Foo.java:5)
at stackoverflow.foo.Foo.<init>(Foo.java:5)
at stackoverflow.foo.Foo.<init>(Foo.java:5)
at stackoverflow.foo.Foo.<init>(Foo.java:5)
There is basically something going on with the constructor (line 7) and the reference variable assignment (line 5).

I theorize that this error occurred because of:
a) Recursion: I googled 'stackoverflowerror java' and the top result displayed an answer over at (not surprisingly) stackoverflow.com. Most of the posters there said a Stackoverflow error usually occurs when there is some kind of recursion (i.e. a function/method calling itself repeatedly). At this point, I do not know much about recursion but I suspect that recursion may be the reason why this error occurred but I am not for certain.
b) The 'new' keyword: I know that we instantiate objects of a class with the 'new' keyword and the 'new' operator will return a reference to the object it created. Source:Java Tutorials - Creating Objects
In addition, on page 242 of Head First Java, it states

the key feature of a constructor is that it runs before the object can be assigned to a reference.


With that being said, I suspect that since I had already assigned my object to a reference variable but the constructor will run before the object can be assigned, perhaps this will create some type of recursion. However, I'm not for certain on this.

Below are some solutions that I have found that will make the code work. But, this does not answer the why.
Solution #1 - Comment out or remove the reference variable assignment.

The console will display Doing something...

Solution #2 - Assign the reference variable locally.

In this case, the console will display the result twice. Not sure why it does that, however.
Doing something...
Doing something...

Basically, there are 3 questions I am curious about:
1) Does the compiler display a StackOverflowError message in the original code because of recursion or is it something else?
2) If it is recursion, how is the code "calling itself"?
3) When I assign the reference variable locally (although, I did not use it) like in solution 2, why does the compiler display the result twice?

I know that this post is long and I appreciate any insight that you all can provide for me. Thanks everyone and have a great day.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1/2. Yes, it's recursion. Specifically, your line Foo f = new Foo(); And the important point is that this is declaring an instance variable of the Foo class.

Which means that whenever you create a Foo, inside that it creates another Foo. Inside that it creates another Foo. And so on. Think of it being like a Matryoshka doll, except it goes on for ever. Eventually the program runs out of the space it allocates for keeping track of the nested calls, and throws the exception.

3. Every time you have the code new Foo(), it creates a new Foo object. Which means that in your final example, it prints the result twice because you're creating two separate objects.
 
Jason Hardaway
Greenhorn
Posts: 26
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:3. Every time you have the code new Foo(), it creates a new Foo object. Which means that in your final example, it prints the result twice because you're creating two separate objects.


I understand how this works now. Thanks

Matthew Brown wrote:1/2. Yes, it's recursion. Specifically, your line Foo f = new Foo(); And the important point is that this is declaring an instance variable of the Foo class.

Which means that whenever you create a Foo, inside that it creates another Foo. Inside that it creates another Foo. And so on.


I have 2 more questions:
1) What exactly is creating the Foo object? Is it the syntax new Foo by itself or Foo f = new Foo.
2) When you say this is declaring an instance variable, I would like to know how is this so because I'm still a little confused about reference variables.
For example:
If I were to say int x = 5; I know that the value of 5 is stored into the integer and x is an instance variable.
Also, If I were to say Foo f = new Foo(); I thought that f is of type Foo and is just a reference to new Foo(). I just don't see how it is creating objects repeatedly.

Once again, I appreciate any assistance.
 
Greenhorn
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) Does the compiler display a StackOverflowError message in the original code because of recursion or is it something else?
2) If it is recursion, how is the code "calling itself"?
3) When I assign the reference variable locally (although, I did not use it) like in solution 2, why does the compiler display the result twice?



1. Yes, it displays because of a recursion error.
2. Well like this: we create a new Foo object and when this object is created a new Foo object is created "in it", and in this one again another Foo object is created and so on until in explodes... well not the explode part.
3. Because you have created 2 Foo objects so the method doSomething will be called once for every object you have made.
 
Mihai Boisteanu
Greenhorn
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) What exactly is creating the Foo object? Is it the syntax new Foo by itself or Foo f = new Foo.
2) When you say this is declaring an instance variable, I would like to know how is this so because I'm still a little confused about reference variables.



1. It is the sintax new Foo(). When you write Foo f = new Foo() you just create a new object Foo and also reference that object with this f.
2. Instance variables are any variables, without "static" field modifier, that are defined within the class body and outside any class's methods body.
The only way you can access an object is through a reference variable. A reference variable is declared to be of a specific type and that type can never be changed. Reference variables can be declared as static variables, instance variables, method parameters, or local variables.

I suggest you start reading some books or some online documentation until you fully understand this.
 
Jason Hardaway
Greenhorn
Posts: 26
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mihai Boisteanu wrote:
1. It is the sintax new Foo(). When you write Foo f = new Foo() you just create a new object Foo and also reference that object with this f.
2. Instance variables are any variables, without "static" field modifier, that are defined within the class body and outside any class's methods body.
The only way you can access an object is through a reference variable. A reference variable is declared to be of a specific type and that type can never be changed. Reference variables can be declared as static variables, instance variables, method parameters, or local variables.



I apologize for the confusion but I think you misunderstood me. I understand how reference variables work but I did not know if the reference variable itself was making the recursion or if it was the new Foo() part. But yourself and Matthew have cleared that up for me now. Thank you.

I suggest you start reading some books or some online documentation until you fully understand this.


I have already read Head First Java & SCJP from front to back (although I am far from a genius). However, these did not cover recursion so I am unfamiliar with that topic like I said in my original post.
At any rate, I thank you all for your help. It is greatly appreciated.
reply
    Bookmark Topic Watch Topic
  • New Topic