Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default constructor

 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
->"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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Statement-1 makes Statement-2 false sometimes.
 
Stephan van Hulst
Bartender
Pie
Posts: 5414
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks every one, I have got the point now..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic