• 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

why passing this in the contructor?  RSS feed

 
Ranch Hand
Posts: 484
10
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went trough some code well written, following the so called Hollywood principle, do not call us, I will call you. In other words a code writenn with the oncle Bob principles that what is inside does not know what is outside.

and I found that the view call presenter instantiating the view with the keyword this.

example

what are the reason of passing this as argument? which advantages I get?
 
author
Posts: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Giovanni Montano wrote:
what are the reason of passing this as argument? which advantages I get?



There nothing advantageous or disadvantageous about using the this reference. You are simply fulfilling the contract (interface) of the constructor. The constructor takes a certain type of instance, and the calling code happens to be part of the instance (either constructor or method) that is to be passed.

Henry
 
Giovanni Montano
Ranch Hand
Posts: 484
10
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Giovanni Montano wrote:
what are the reason of passing this as argument? which advantages I get?



There nothing advantageous or disadvantageous about using the this reference. You are simply fulfilling the contract (interface) of the constructor. The constructor takes a certain type of instance, and the calling code happens to be part of the instance (either constructor or method) that is to be passed.

Henry


thanks, but i do not get that I am fulfilling the contract interface of the constructor. I understand the concept you mean, but there is any interface keyword involved right?
I know is possible to define a contructor inside an interface, but i do not see any interface, is your a metaphorical way of speaking? Thanks
 
Henry Wong
author
Posts: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think you are reading too much into my response.

By interface, I mean the signature, or the parameters. The constructor expects a certain instance to be passed as a parameter. And the calling code, happens to have a reference to it, which is itself.

There is no special feature, or use-case, here. It just happens to be accessible via this because that is how the application is written. You could have changed the algorithm (or code location) and then you would need a different reference for the parameter.

Henry
 
Giovanni Montano
Ranch Hand
Posts: 484
10
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:
I think you are reading too much into my response.

By interface, I mean the signature, or the parameters. The constructor expects a certain instance to be passed as a parameter. And the calling code, happens to have a reference to it, which is itself.

There is no special feature, or use-case, here. It just happens to be accessible via this because that is how the application is written. You could have changed the algorithm (or code location) and then you would need a different reference for the parameter.

Henry


Ok thanks
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!