• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Why to instantiate a class within itself??

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Java Gurus,
I was looking at some code written by someone...
and came across this

Why did he instantiate the class within itself? Here temp is a type of employee. But by doing so isnt the object in kind of a loop. How is this useful? I am confused .. Is this a kind of inheritance?? please help
Thanks
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This type of class structure is not in a loop. The instance member temp is assigned a value null. It has to be explicitly set to a new EmpInfo object whose member temp will in turn be null.
This pattern is typically used to create a Linked List.
On the other hand,
Class EmpInfo {
private EmpInfo e = new EmpInfo();
}
will be a loop because when we create an instance of this class, there will be a recursive creation of objects that results in a stack overflow error.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic