This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Object Initialization

 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • 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



 
Sheriff
Posts: 7647
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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: 69799
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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®.
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic