This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof doubt" Watch "instanceof doubt" New topic
Author

instanceof doubt

aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111
Hi,




can any explain why the above code throws exception
(Exception in thread "main" java.lang.StackOverflowError)


Thanks in Advance
samura

Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi samura,

the problem is the b variable in BinstanceTest. When you instantiate a BinstanceTest, a second BinstanceTest is instantiated to initialize the b variable which instantiates a BinstanceTest to initialize the b variable which ...
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111


Thanks Manfred,




Operands and Results Using instanceof Operator

In pg 286 (k&B book) table 4.1

I didn't get the 5th row..

1st operand = Foo[]
instanceof operand = Foo,Bar,Face
result = false

why the result is false?

Thanks!
samura





[ August 23, 2007: Message edited by: samura babu ]
[ August 23, 2007: Message edited by: samura babu ]
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
First operand is an array of object and second operand is the object itself.

MyClass obj;
MyClass arrObj[];

You can not compare obj with arrObj. However comparison between "arrObj[someIndexHere]" and "obj" would be legal.
[ August 23, 2007: Message edited by: Akhilesh Trivedi ]

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Originally posted by Manfred Klug:
Hi samura,

the problem is the b variable in BinstanceTest. When you instantiate a BinstanceTest, a second BinstanceTest is instantiated to initialize the b variable which instantiates a BinstanceTest to initialize the b variable which ...


That was really deadly manfred, good catch.

Samura! Good practice would be to have member variable declarations in the class, and initializations at the place/constructor/methods where they are necessary.
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111


Thanks Akhilesh!


regards
samura
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof doubt
 
Similar Threads
Code within a curly braces
Weird behavior of instanceof
inheritance for Object class
protected field
Reference Variable Casting