• 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

constructor calling this() and super() problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.what does this(); do when it is used inside a constructor ?
2.can it call constructor of super class or just the other overloaded constructor in the same class ?
3.what does super(); do when used inside a constructor ?
4.does it call the super class constructor and can it call overloaded constructor of the superclass ?

eg:



}




what would be the output if
new a();
new b();
new b(a);

are used separately int the above program ?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you tried it?
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you call this() or super(), it must be the first executed line of the
constructor; you may use one or the other, but not both. And yes,
this() calls another constructor in the same class.

Jim ... ...
 
Ranch Hand
Posts: 30
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Jim, for the code you have given

for line 3 it would be a recursive call, to it's own constructer
for line 17 as the super class of b is Object class and the object class does not have any constructer with pareameter int, this line wouldn't compile
and line 26 won't compile, what is 'a' which you have passed??

I hope you understood..
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add little more :-

Within Constructors:-

Within constructors , this() or super() should be the first statement, since both cannot be first , therefore only one could reside within a single constructor, but somewhere ,within the same class, there is a constructor which has super() as the first statement. ( either it is added by you or by compiler ). When you add , then super can take many forms i.e. super(), or super(with arguments).


Within Methods :-

Within Methods , super() and this() does not work, but but but ....... super.variable_name , this.variable_name, super.method_name(), this.method_name() does work .
Moreover withing methods , there is no restriction for super and this to be the first statement, and moreover there could be this and more both residing within the same method anywhere and may be more than one time.

Thanks
Sahil Rally
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method calls this() and super() are restricted to first-line use in constructors,
while "this" and "super" are object references. And don't use this() in every
constructor. It will cause stack overflow because super() will never be called.

Jim ... ...
 
reply
    Bookmark Topic Watch Topic
  • New Topic