• 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

Object of Class N declared inside Class N itself.

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This gets compiled.
But i dont understand the logic. A class declaring objects of its own inside it, when the definition of that class isnt finished yet.
Can someone give me 2-3 lines of tutorial about what is happening
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a valid instance member called ob, whose type is Test, initialized to "new Test()", and whose access type is default.

when the definition of that class isnt finished yet.


It is finished. Would it be more readable that way ?
 
shaun jacob
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the tip.
It makes more sense now
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just never try to create instances. If you create an instance, that will create an instance, that will create an instance, and so on until a StackOverflowError is thrown.
 
Marshal
Posts: 79469
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please always tell us what the question is about.
 
shaun jacob
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please always tell us what the question is about.



Sorry. Ive corrected the mistake.Now Back to the question.

But doesnt it sound recursive ?
According to this Class definition, when an instance object of this Class is created, another instance object of the same Class is needed to be created inside the second. For the second object , a third one is needed to be created inside and it needs to go on like this.

I know it is a problem with my understanding of this. Anybody correct me.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're correct. It is recursive. You could "fix" it like this:
 
shaun jacob
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:You're correct. It is recursive. You could "fix" it like this:



OK. Here new Test() wont get executed when a Class object is created and we need to explicitly call it.

Mine is not a specific coding problem but rather a probelm with my understanding. So what is actually going on in my first example.

How much Test objects will be created when we create the first instance of Test Class?
 
shaun jacob
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Just never try to create instances. If you create an instance, that will create an instance, that will create an instance, and so on until a StackOverflowError is thrown.



Sorry. Late to notice this.

Can anybody give me the smallest code to cause this StactOverFLowError
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that would be this:

 
shaun jacob
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. Thanks for the code.

So, it is recursive and will cause overflow errors if executed.
 
Rob Spoor
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly. So the answer to your questions on the number of instances: in theory it is infinite, in practice it is until the StackOverflowError occurs.
reply
    Bookmark Topic Watch Topic
  • New Topic