• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Default Access on a Constructor

 
Greenhorn
Posts: 24
Postgres Database VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings Moose People:

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...
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Terry Tucker wrote:Greetings Moose People:

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.

 
Terry Tucker
Greenhorn
Posts: 24
Postgres Database VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andy Jack wrote: . . . Create a package called 'src'. In that, create 2 packages ex and dummy. . . .

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
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is very unusual. Most people faced with learning a language and learning an IDE together go down with information overload and learn neither.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Terry Tucker
Greenhorn
Posts: 24
Postgres Database VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Folks:

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...
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic