File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Default constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Default constructor " Watch "Default constructor " New topic
Author

Default constructor

swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

->"The access modifier implicitly assigned to the default constructor is the same as that assigned to the class" (true)
->"The default constructor is always given default package access." (false) which I thought to be true, as a member with no access modifier have default or friendly access..

...will you please relate these lines with the two scenarios specified below. I have got confused here.
1>2>

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

The Statement-1 makes Statement-2 false sometimes.


Mohamed Sanaulla | My Blog
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

It's a bit confusing, because they use the word default to refer to different meanings here.

What they mean by default constructor, is the constructor that is implicitly inserted by the compiler. In the first piece of example code you gave, there is a default constructor inserted by the compiler that will have the same access modifier as the class Ranch. Since Ranch is package private, so will the constructor be.

In your second example, there is no default constructor. You explicitly created a constructor yourself, so it will have the access modifier you assigned to it. In this case it happens to be package private.

Note that the two statements in bold are mutually exclusive. If one is true, the other must be false.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

To be precise with terminology: your second scenario is not a default constructor. It is a no-argument constructor that you have provided. So the statements don't apply to it.
himanshu.harish agrawal
Ranch Hand

Joined: Oct 18, 2010
Posts: 47

Hello Swaraj,

Both the statements are correct. Before you think how, you need to get an important concept.

A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own noarg constructor.

So, if you are writing your own constructor (even no-arg constructor) then both those statement will not come into picture. And is you are not writing then first statement will hold and second will not because JLS have never mentioned that "The default constructor is always given default package access.". .

It is because of this wrong statement that you are getting confused. To get a confirmation on this try to create two classes in one package (one public class and other default level class but none of them is declaring constructors) and then from some other package try to create an instance of them. Now, you will find that for the public class you can create an instance but for default level class you cannot, this is because of the access modifier associated with their constructors.


I think i need not to elaborate the above conclusion because statement in bold and italics should be enough to answer your question.

Please feel free if you have further doubts.


Thanks.
himanshu.harish agrawal
Ranch Hand

Joined: Oct 18, 2010
Posts: 47

himanshu.harish agrawal wrote:Hello Swaraj,

Both the statements are correct.

Thanks.


Sorry, first statement will hold true and second is wrong.
Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 146

Swaraj,

Consider the statement present on K&B - Chapter 2 , page 136:
...The default constructor has the same access modifier as the class...


It make things more clear for me.


Adolfo Eloy
Software Developer
OCPJP 6
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

thanks every one, I have got the point now..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Default constructor