This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
->"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.
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.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
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.