• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Super and this within same method/constructor

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please explain:
1. Can we have both super() and this within the same method/constructor??
2. I read it's not possible, but when i tried running the following code, it runs fine:

Can somebody throw some light on this?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




Can somebody throw some light on this?


Swati,
u cannot use super( <args> ) and this( <args> ) within the same constructor.
NOTE: super( <args> ) invokes a constructor in the parent and this( <args> ) is used to invoke another constructor within the same class.
In your code above u access the super() constructor invocation and the this object (not the constructor call). perfectly legal.
Bijesh
[ March 19, 2004: Message edited by: Bijesh Krishnadas ]
[ March 19, 2004: Message edited by: Bijesh Krishnadas ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Swati,
Even I too read that both super() and this() cannot be placed in the constructor, thanks for posting this Qn even me too was in same impression.
But they both can be in the same constructor, just make sure that super() must be the first statement to be placed in the constructor, or else it may throw a compile time error.
Cheers
Ravi
 
Swati Singhal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bijesh. I am clear about it now.
 
Bijesh Krishnadas
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by RaviKumar Golagani:
Hi Swati,
.....
But they both can be in the same constructor, just make sure that super() must be the first statement to be placed in the constructor, or else it may throw a compile time error.
.....


Ravi,
I think u misunderstand. super() and this() cannot be present in the same constructor. In swati's code we don't have super() and this() in the same constructor. We have only super(), this is used just as an object.
Use of both super() and this() are restricted by the condition that they can be used only as the first statement of a constructor. Both cannot be the first statement at the same time and so cannot be used together in the same constructor.
Am I making this too confusing?
Bijesh
 
RaviKumar Golagani
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krishnadas
Yes, I was in a overlook....
Thanks
Ravi
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic