Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object of Class N declared inside Class N itself.

 
shaun jacob
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for the tip.
It makes more sense now
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that would be this:

 
shaun jacob
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Thanks for the code.

So, it is recursive and will cause overflow errors if executed.
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic