File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Concstructors withing constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Concstructors withing constructors" Watch "Concstructors withing constructors" New topic
Author

Concstructors withing constructors

Sumanth Shanbhag
Greenhorn

Joined: May 10, 2007
Posts: 13
Hi All,

Could someone please tell me if we could have one constructor declared inside another constructor? What are its potential disadvantages and advantages, if any?

Regards,
Sumanth
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
No, you cannot have a method declared directly inside a method.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
Sumanth Shanbhag
Greenhorn

Joined: May 10, 2007
Posts: 13
So, i guess one of the primary advantages of "this" keyword is code reusability?
Thanks Marc!
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
'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.

Sid
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14149
    
  18

Originally posted by Keith Lynn:
No, you cannot have a method declared directly inside a method.

But this doesn't answer the question, because a constructor is not a method.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Sumanth Shanbhag
Greenhorn

Joined: May 10, 2007
Posts: 13
Sid,

Thanks for your inputs, but i was referring to Marc's answer.
He was talking about having "this" call another constructor from
within a constructor.

Regards,
Sumanth
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Jesper Young:

But this doesn't answer the question, because a constructor is not a method.


Technically, no. But the principle is the same. The body of the constructor can't directly have a method declaration or constructor in it.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Concstructors withing constructors