Results 1 to 2 of 2
  1. #1
    genkuro is offline Member
    Join Date
    Dec 2010
    Rep Power

    Default Default / package-private access

    Is package-private a legitimate concept? I mean, forgive the sacrilege, but it feels like an accidental oversight that became a de facto piece of a spec out of necessity. "Hey, did you know this code compiles if you don't use private, protected, or public? Yeah, so do these other 10,000 programs. Uh, let's just write it into the spec as default behavior."

    Default access is unnamed of course which is syntactically bad and counter intuitive. I'd expect default to sort out to private myself. And I'll argue that package-private is a little uglier that C++'s friend syntax. Package-private gets tangled into the file system after all.

    Then there are the design trade-offs. Encapsulation spills out with default access. And any immutable contract is broken with default access. Maybe these types of contracts can be made at the package level, not sure.

    So, here's the simple question to those with 10+ years of Java programming. Do you use default access deliberately to allow other objects in the same package to manipulate your object directly?

    How would you rank default access: never use, use extremely cautiously and only when it makes sense, or use all the time?

    Many thanks for all insight.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    Quote Originally Posted by genkuro View Post
    How would you rank default access: never use, use extremely cautiously and only when it makes sense, or use all the time?
    I use package/default scoped classes when it's none of the world's business to know of those classes but they're very handy for the other (public) classes in the same package. When only one class uses my package scope class I sometimes refactor the class to be a (static) nested class of that other class.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. has private access error message
    By ibrahimyoussof in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 04-13-2010, 04:01 PM
  2. Default Access (package access) confusion
    By gauravrajbehl in forum New To Java
    Replies: 1
    Last Post: 11-18-2009, 10:48 AM
  3. Replies: 1
    Last Post: 03-10-2009, 01:12 PM
  4. How to access private data types from public classes?
    By kevzspeare in forum New To Java
    Replies: 3
    Last Post: 03-07-2009, 04:19 AM
  5. importing from default package?
    By diggitydoggz in forum New To Java
    Replies: 6
    Last Post: 01-06-2009, 01:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts