• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Concstructors withing constructors

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you cannot have a method declared directly inside a method.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, i guess one of the primary advantages of "this" keyword is code reusability?
Thanks Marc!
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic