• 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 Initialization

 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,  I read OCA Java SE 8 Programmer I Certification Guide. Where came through the mock test. I was confused in MEQ-18 question. See below code



Explanation: Code throws StackOverflowError. It said recursive call due to instance variable emp; Can anyone please elaborate more how recursive call in an instance variable emp ?

Thank you
Salman



 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, salman,

As you see, at line 8 you have an object instantiation = new Emp(), so an Emp gets created. Now if you go back to line 2, you'd see, that Emp class contains a field which gets initialised upon Emp initialisation, so Emp get's created, which apparently has Emp initialised upon its initialisation, which has Emp initialisation itself, which has...

Get an idea?
 
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line number 2 is called is called recursively, any attempt to create Emp object goes in to infinite loop due to this line number 2.
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lakshman Arun wrote:. . . infinite loop due to this line number 2.

No, that is infinite recursion, not an infinite loop. Both have the same semantics: incorrect results, but you will see different effects in Java®.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic