This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor..." Watch "A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor..." New topic
Author

A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor...

Yin Stadfield
Ranch Hand

Joined: Oct 13, 2012
Posts: 35

Hi everyone. I'm currently reading Chapter 2 of K&B SCJP 6 Study Guide and on page 134, I stumbled upon this paragraph:

"A no-arg constructor is not necessarily the default (i.e., compiler-supplied)
constructor, although the default constructor is always a no-arg constructor."

I don't know if it's because I wasn't able to get enough sleep yesterday, but that paragraph wasn't able to pass across its meaning to me.

"A no-arg constructor is not necessarily the default (i.e., compiler-supplied)
constructor" - does it mean that compiler also supplies default constructors with arguments???

"although the default constructor is always a no-arg constructor" - but here it says that the constructor the compiler generate by default doesn't have arguments.



Any enlightenment or examples provided would be appreciated .

Yin
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

Hi Yin,

Yes, I would say that the statement is quite confusing at first read.

Lets break it in 2 parts:

Part 2: The default constructor is always a no-arg constructor. I guess this is quite straight-forward. If a class does not have any constructor, then 'default' constructor will be the one provided by compiler - which will be without arguments (the constructor, not the compiler ). If a class does already have one/more constructor(s), then compiler will not provide any constructors. Thus, if a compiler provides a constructor, it will always be a no-arg constructor.

Part 1: A no-arg constructor is not necessarily the default (i.e., compiler supplied). Here, what if you write your own no-arg constructor? In such case, compiler will not provide any constructor at all (and hence, the no-arg constructor is written by you, and not supplied by compiler).

I hope this is clear.

Bottom line:
1) If a class does not have any constructor at all, then compiler will provide its own constructor (which will be a no-arg constructor).
2) If a class have at least one explicit constructor (which may or may not be no-arg constructor), then compiler will not implicitly provide any constructor at all.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Yin Stadfield
Ranch Hand

Joined: Oct 13, 2012
Posts: 35

Anayonkar Shivalkar wrote:Hi Yin,

Yes, I would say that the statement is quite confusing at first read.

Lets break it in 2 parts:

Part 2: The default constructor is always a no-arg constructor. I guess this is quite straight-forward. If a class does not have any constructor, then 'default' constructor will be the one provided by compiler - which will be without arguments (the constructor, not the compiler ). If a class does already have one/more constructor(s), then compiler will not provide any constructors. Thus, if a compiler provides a constructor, it will always be a no-arg constructor.

Part 1: A no-arg constructor is not necessarily the default (i.e., compiler supplied). Here, what if you write your own no-arg constructor? In such case, compiler will not provide any constructor at all (and hence, the no-arg constructor is written by you, and not supplied by compiler).

I hope this is clear.

Bottom line:
1) If a class does not have any constructor at all, then compiler will provide its own constructor (which will be a no-arg constructor).
2) If a class have at least one explicit constructor (which may or may not be no-arg constructor), then compiler will not implicitly provide any constructor at all.

I hope this helps.


Hi Anayonkar! It sure does make things more clearer to me. I got it. Thanks!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10171
    
    8

There is a fine distinction here. The no args constructor has, well no arguments. However the default constructor is a no args constructor and also has an empty constructor body.
e.g.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3254
    
    2
Another distinction - accessibility (i.e. public/private/protected) of the default constructor is same as that of the class, while a non-default no-arg constructor can have any access modifier


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Maneesh Godbole wrote:There is a fine distinction here. The no args constructor has, well no arguments. However the default constructor is a no args constructor and also has an empty constructor body.
e.g.

But only if it is implicitly created by the compiler because the coder hasn't provided any constructors. If the coder explicitly wrote that code then it would be a no-args constructor but not a default constructor.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor...
 
Similar Threads
a question about default constructor
Why Is a No-Arg Constructor Required Here?
Default constructor
constructor