This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
You cannot "declare" or define a constructor within a constructor, but you can call another constructor from within a constructor.
Within a constructor, the keyword "this" along with the appropriate arguments can be used to call an overloaded version of the same constructor. The reason is usually to avoid code duplication. For example...
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: May 10, 2007
So, i guess one of the primary advantages of "this" keyword is code reusability? Thanks Marc!
'this' keyword is not meant for code reuse. It is just meant to refer the current object. When in a class, you call a non-static member from a non-static method, you need not prefix it with 'this.', because the compiler understands it. Though, for better readability, the keyword can be used.
Originally posted by Sumanth Shanbhag: So, i guess one of the primary advantages of "this" keyword is code reusability? ...
Well, that's just one way to use "this" within a constructor (which I thought might relate to your question of a constructor "within" a constructor), but I don't think I would call it a "primary advantage." As pointed out above, "this" also references the calling instance, so it's often used in constructors to differentiate an instance variable from a local variable with the same name. For example...