Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Concstructors withing constructors

 
Sumanth Shanbhag
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you cannot have a method declared directly inside a method.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Sumanth Shanbhag
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, i guess one of the primary advantages of "this" keyword is code reusability?
Thanks Marc!
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'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
Posts: 15288
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sumanth Shanbhag
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic