• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this a runtime error ?  RSS feed

 
Latif Ben
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Why this happens when I try to run these codes:







I got something like this :

at First.<init>(First.java:4)
at Second.<init>(First.java:18)
at First.<init>(First.java:4)
at Second.<init>(First.java:18)
at First.<init>(First.java:4)

at Two.<init>(One.java:16)
at Two.<init>(One.java:16)
at Two.<init>(One.java:16)
at Two.<init>(One.java:16)
at Two.<init>(One.java:16)

at Cls.met(Cls.java:7)
at Cls.met(Cls.java:7)
at Cls.met(Cls.java:7)
at Cls.met(Cls.java:7)
at Cls.met(Cls.java:7)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's hard to say, since you didn't tell us what class you are running. All three you provide have a main, so any could be what you run. it looks like you may actually be running all three, one at a time, but simply lumped your results into out output. So really, i'm just guessing here.

in First, you create a Second object. When you make that, you have to create a First object. When you make that, you have to make a Second object. When you make that, you have to make a First object...

In One, you make a Two object. To make that, you need to make a Two object. To make that, you have to make a Two object. To make that...

In Cls, you call the met() method. That method calls the met() method. That method calls the met() method. That method...

in each case, it appears you have infinite recursion.
 
Latif Ben
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:it's hard to say, since you didn't tell us what class you are running. All three you provide have a main, so any could be what you run. it looks like you may actually be running all three, one at a time, but simply lumped your results into out output. So really, i'm just guessing here.

in First, you create a Second object. When you make that, you have to create a First object. When you make that, you have to make a Second object. When you make that, you have to make a First object...

In One, you make a Two object. To make that, you need to make a Two object. To make that, you have to make a Two object. To make that...

In Cls, you call the met() method. That method calls the met() method. That method calls the met() method. That method...

in each case, it appears you have infinite recursion.


Thanks for your answer
actually im running every program separately, not one at a time
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really, there is no need for the bold tags. it looks like you are shouting at us.

Part of being a good programmer is being detail oriented. Telling us in the first post you were running three separate programs would have helped. That was my only point there.

But just to clarify, in each case, you have infinite loops. Building a new First object requires you to build a new Second object. But building a new Second object requires you to build new First object. so the JVM keeps going back and forth, building a new object of each type, until you run out of memory. My guess is that you got a lot more of this:
at First.<init>(First.java:4)
at Second.<init>(First.java:18)
than you posted. It probably scrolls off the top of your cmd window (assuming windows), so you don't see the out of memory error.

You have similar problems with the other two classes. Your Two class requires a new Two object to be build inside every Two object. It's like a set of nested Russian dolls.
 
Singh Harmeet
Ranch Hand
Posts: 115
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in simple words this is the cyclic call by constructors , this is stack over flow Error.
because First Class Create Second Class Object and Second Class Create First Class Objct.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!