*
The moose likes Beginning Java and the fly likes Object of Class N declared inside Class N itself. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object of Class N declared inside Class N itself. " Watch "Object of Class N declared inside Class N itself. " New topic
Author

Object of Class N declared inside Class N itself.

shaun jacob
Greenhorn

Joined: Jan 11, 2010
Posts: 7


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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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 ?


[My Blog]
All roads lead to JavaRanch
shaun jacob
Greenhorn

Joined: Jan 11, 2010
Posts: 7
Thanks for the tip.
It makes more sense now
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Please always tell us what the question is about.
shaun jacob
Greenhorn

Joined: Jan 11, 2010
Posts: 7
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

Joined: Oct 25, 2008
Posts: 2700

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


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
shaun jacob
Greenhorn

Joined: Jan 11, 2010
Posts: 7
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

Joined: Jan 11, 2010
Posts: 7
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

Joined: Feb 10, 2009
Posts: 710
I believe that would be this:



SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
shaun jacob
Greenhorn

Joined: Jan 11, 2010
Posts: 7
OK. Thanks for the code.

So, it is recursive and will cause overflow errors if executed.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object of Class N declared inside Class N itself.
 
Similar Threads
Recursion
how to convert a .jsp to a .java/.class file
instanceof
integer palindrome 'no strings'
Regular Expression: finding multiple lines