Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof doubt

 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thanks Akhilesh!


regards
samura
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic