Results 1 to 2 of 2
  1. #1
    gauravrajbehl is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default Default Access (package access) confusion

    Hi eveybody,

    I am new to java and as I was reading, I came accross the default access modifier i.e. package level access modifier which is used to restrict only the classes in the same package to access the class.

    I've understood the concept and how it is applied technically but I am not able to figure out in which situations package level access would fit in. Can someone help me giving a short scenario or a practical example where package level access can be used.

    thanks

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Packages represent a collection of classes which "work together". Package access allows these classes to have fields, functions, and classes even, that can be only accessed from within the same package.


    If you check the source code for String (in java.lang), you'll notice a package constructor, with a note "// Package private constructor which shares value array for speed.".

    An example of the constructor's use is in the String class' substring method. Since Strings are immutable (cannot change), a substring doesn't have to copy the string's characters. Instead, a String is created, with an offset and count that refer to the substring, while keeping the same char[] as the original String.

    Doing this offers better performance, especially for longer strings, and better memory too, since there is no need to copy the characters in the substring to a different array.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. default access modifier
    By vishnugch in forum New To Java
    Replies: 1
    Last Post: 08-17-2009, 01:48 PM
  2. Regarding default access modifier?
    By makpandian in forum New To Java
    Replies: 2
    Last Post: 03-14-2009, 09:21 AM
  3. Replies: 1
    Last Post: 03-10-2009, 02:12 PM
  4. Method access or field access
    By carderne in forum New To Java
    Replies: 2
    Last Post: 12-06-2008, 07:20 PM
  5. Replies: 1
    Last Post: 08-07-2007, 08:51 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
  •