Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

an object using it self

 
Syamsul Hussin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, is it possible for an object to to use itself as a parameter of a method. for example:-

constructor of object1 constructs an object2 but since object2 has object1 as one of its field, then object1 has to to send itself as the parameter of object2's constuctor.
is that possible?thanks
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. There is no reason why a class can't have a reference to an instance of the same class. The singleton is a prime example of that. this can be used in any context that calls for a reference to the enclosing class or any of its superclasses. The only caveat is that in a constructor the super constuctor must be called before this can be referenced either implicitly or explicitly, in other words you can't do something like this:
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no problem doing this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic