aspose file tools*
The moose likes Associate Certification (OCAJP 7/8) and the fly likes super() and this() in same constructor method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7/8)
Bookmark "super() and this() in same constructor method" Watch "super() and this() in same constructor method" New topic
Author

super() and this() in same constructor method

Mustakimur Rahman
Greenhorn

Joined: Oct 12, 2013
Posts: 13

When I try this:


This show error: super() must be the first statement.
So, when I try this:



It show error again: this() must be the first statement.

So, what will be the perfect order if I try both in a same constructor?
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1049
    
  15
Mustakimur Rahman wrote:When I try this:
------

This show error: super() must be the first statement.
So, when I try this:
--------------
It show error again: this() must be the first statement.

So, what will be the perfect order if I try both in a same constructor?


A constructor can have either this or super instruction ( but not both ) and when it has one of these instructions, it must be the first instruction in the constructor.

HIH
Chan.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5543
    
  13

FYI: there is no such thing as a "constructor method". It's a constructor or a method, but the combination is impossible.

Time for another pop quiz: what's the difference(s) between a constructor and a method? How can you distinguish a constructor from a method?


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Mustakimur Rahman
Greenhorn

Joined: Oct 12, 2013
Posts: 13

The constructor and method can be differentiate by its return type. If it have, then its method else its constructor.
But, can anyone give me some details why it is not allowed to have both super() and this() is a same constructor?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4465
    
    8

Mustakimur Rahman wrote:But, can anyone give me some details why it is not allowed to have both super() and this() is a same constructor?


super() is always going to get called eventually. If you write this code:
Then there's an implicit call to super() in the constructor that takes a String. For every constructor, either:
- there is an explicit call to this(...) , or
- there is an explicit call to super(...), or
- an implicit call to super() is inserted by the compiler.

So if what you were trying to do was allowed then the superclass constructor would end up being called twice.

Mustakimur Rahman
Greenhorn

Joined: Oct 12, 2013
Posts: 13

Matthew Brown wrote:
Mustakimur Rahman wrote:But, can anyone give me some details why it is not allowed to have both super() and this() is a same constructor?


super() is always going to get called eventually. If you write this code:
Then there's an implicit call to super() in the constructor that takes a String. For every constructor, either:
- there is an explicit call to this(...) , or
- there is an explicit call to super(...), or
- an implicit call to super() is inserted by the compiler.

So if what you were trying to do was allowed then the superclass constructor would end up being called twice.



But, I can use super() explicitly if I don't use this().
Why not in this case, the compiler show error from your point?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4465
    
    8

Mustakimur Rahman wrote:But, I can use super() explicitly if I don't use this().
Why not in this case, the compiler show error from your point?

If you call super(), then the superclass constructor is called.
If you call this(), then another constructor in this class is called, and that will call super().
So either is fine. But if you call both then super() will end up being called twice. Why would you ever want this to happen?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5543
    
  13

A call to another constructor of the same class (using this) or to a constructor of the parent class (using super) must always be the 1st statement of a constructor. So using both is simply impossible.

If it was allowed to invoke both this and super in 1 constructor, super() will be called twice.
Mustakimur Rahman
Greenhorn

Joined: Oct 12, 2013
Posts: 13

I get the point now. Thanks all.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5543
    
  13

Mustakimur Rahman wrote:But, I can use super() explicitly if I don't use this().

That's correct! You have to choose: an explicit call to this(), an explicit call to super() or nothing (and then the compiler will insert a call to super() for you)

Mustakimur Rahman wrote:Why not in this case, the compiler show error from your point?

Because that would require lots of boiler-plate code from the developer. This code will perfectly compile and the compiler will add a lot of code for you.


If the compiler would generate errors like you propose, you'll need to write the following code in order to get exactly the same as the code above.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: super() and this() in same constructor method