• Post Reply Bookmark Topic Watch Topic
  • New Topic

What are different ways to use/initialize constructors?  RSS feed

 
Mark Andrew Davis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

I hope you are all well!! I was wondering, what are different ways to use/initialize a constructor and what are examples/purposes you would you them over another?

Apart from I don't really know many others and I do find myself hitting some walls due to such, so thought I would ask and expand my knowledge.

Thanks,
Mark.D
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What roadblocks are you hitting?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following the new operator, are there any other ways to invoke a constructor at all?
From inside the constructor, there are this(...); and super(...); but their use is subject to strict rules. I can't think of any others.
The arguments list must match the parameter list of a constructor with suitable access.
 
David Simkulette
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's factories and injection and a lot of code patterns AROUND constructors  with special names like Builder and Abstract Factory etc. etc. There's even creating an instance of an object via reflection (which is what injection does). All but the last all rely on calling new() at the end fo the day. 

This will give you some proper nouns you can pursue :

https://en.wikipedia.org/wiki/Creational_pattern
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Simkulette wrote:. . . There's even creating an instance of an object via reflection . . .
But I woiuldn't recommend reflection if you can avoid it.

All those other techniques end up calling new Foo(...) somehow.
 
David Simkulette
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:But I woiuldn't recommend reflection if you can avoid it.



I was never impressed with any of the code injection approaches. I am sure that's because they don't solve any problem I've ever had, so I have no gratitude . 
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!