Default Access on a Constructor

I would like to know if a constructor that has no access modifier explicitly stated behaves the same as a class that is declared without an access modifier; that is, does if have "package access" (default)? What about methods? When no access modifier is supplied, do they have package access?

I would like to know if a constructor that has no access modifier explicitly stated behaves the same as a class that is declared without an access modifier; that is, does if have "package access" (default)? What about methods? When no access modifier is supplied, do they have package access?

Thanks for the input...

When you have little questions like these. Please make code yourself to see how things behave. Create a package called 'src'. In that, create 2 packages ex and dummy.

...this is ok.

but this is not -

If you are not using an IDE to compile and run your code, then please do it. Its good because it shows you errors the moment you make them. This is good for beginners. You can experiment quickly and see results with less effort.
Do command line once you know the language well.

Thanks for the reply and advice. I am using command line and vim for this because the SCJP Study Guide recommended putting aside the IDE. I did create a couple classes as you suggest and have shown that both constructors and methods, without an access modifier specified, behave with "default" access.

Again, thanks for the help...

Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257

posted Jan 31, 2013 15:19:30

0

And please read some good text books for questions like these - head first java, absolute java by walter savitch, thinking in java etc.

Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257

posted Jan 31, 2013 15:25:18

0

Terry Tucker wrote:Thanks for the reply and advice. I am using command line and vim for this because the SCJP Study Guide recommended putting aside the IDE. I did create a couple classes as you suggest and have shown that both constructors and methods, without an access modifier specified, behave with "default" access.

Again, thanks for the help...

Well, the SCJP guide has good reasons to recommend a non-IDE approach. Right now, I guess that your goal is to learn quickly. So, use an IDE and and learn the basics taught in the SCJP book quickly. Once you learn all the basics, you can do all your coding without an IDE.
The SCJP book by kathy sierra and bert bates is excellent. But, do not use that or any SCJP book as your first book to learn Java.

Andy Jack wrote: . . . So, use an IDE and and learn the basics taught in the SCJP book quickly. Once you learn all the basics, you can do all your coding without an IDE. . . .

Not at all. It should be the other way round.

Learn to use an IDE after you are experienced in the use of text editors and the command line or terminal

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44016

33

posted Jan 31, 2013 16:08:33

0

Andy Jack wrote: . . .
/* If you make demo class public, then this line will not work
* and cause an error - The constructor demo() is not visible
*/
. . .

You have got something wrong there. I think you have missed out a not/don’t somewhere.

That class is package‑private with a public constructor; if you change it to a public class, then it will become visible.
And I had to change your // comment to /*...*/ because the long lines are so difficult to read.

You mean a folder called src. That is not a package, otherwise you would have to name the packages src.something.

Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257

posted Feb 01, 2013 00:00:58

0

Campbell Ritchie wrote:

Andy Jack wrote: . . . So, use an IDE and and learn the basics taught in the SCJP book quickly. Once you learn all the basics, you can do all your coding without an IDE. . . .

Not at all. It should be the other way round.

Learn to use an IDE after you are experienced in the use of text editors and the command line or terminal

That is exactly what I had done for the SCJP. I found it to be a serious impediment to my progress. In hindsight, I realized that I could have learned things much faster and then done all the compile, make package, make jar commands for SCJP. If I wanted to learn WAY more than what was covered in the SCJP,
then I would use command line and build tools instead of an IDE. The command line based topics in the SCJP are such that you can do them after you learn the language using an IDE.
YMMV

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44016

33

posted Feb 01, 2013 01:39:05

1

That is very unusual. Most people faced with learning a language and learning an IDE together go down with information overload and learn neither.

Terry Tucker wrote:I would like to know if a constructor that has no access modifier explicitly stated behaves the same as a class that is declared without an access modifier...

No.

Access modifiers, wherever they appear, apply to the thing they are put on - be that a field, class, constructor or method - and, as you appear to know already, the absence of a modifier means "package-private" - ie, it is not visible outside the package in which it's defined.

By extension, that means that if you put it on a class, the class itself will be package-private, whereas if you put it on a constructor, the constructor will be package-private.

So: why might you want to put it on a constructor?

The obvious answer is that, for whatever reason, you don't want anything outside the package using that constructor to create an object; however, there are a couple of other wrinkles to consider:
1. If your package-private constructor is the only one defined for the class, or ALL constructors are defined as package-private, no code outside the package will be able to create an object for that class.
2. Constructors are also part of the inheritance hierarchy; so if your class is not final and the conditions in (1) apply, nothing outside the package will be able to define a class that extends your class, which may be very useful for controlling the development of subclasses.

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here

Thanks much for the discussion. Actually, I am working as a Java programmer using Eclipse in the work environment. I have found that it does indeed help me with my ignorance; however, I find I depend on it too much. I am studying for the Java Certification as my place of employment requires it and I greatly desire it. They hired me without having the certification and without having very much experience with Java at all because I have been a programmer in the transportation industry for the past 20+ years (C and the X-Windows libraries). All the code I wrote in my previous life was with VI

Not only did I have to learn Eclipse (still in progress), but all their libraries as well (still in progress). Add to that, Java Cert.; well, my plate be full

Nonetheless, I appreciate the wisdom and help of the list. I have a lot to learn...