Results 1 to 2 of 2
- 11-18-2009, 10:22 AM #1Member
- Join Date
- Nov 2009
- Rep Power
Default Access (package access) confusion
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.
- 11-18-2009, 11:48 AM #2
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
- By vishnugch in forum New To JavaReplies: 1Last Post: 08-17-2009, 01:48 PM
- By makpandian in forum New To JavaReplies: 2Last Post: 03-14-2009, 09:21 AM
- By jazz2k8 in forum NetworkingReplies: 1Last Post: 03-10-2009, 02:12 PM
- By carderne in forum New To JavaReplies: 2Last Post: 12-06-2008, 07:20 PM
- By cachi in forum JDBCReplies: 1Last Post: 08-07-2007, 08:51 AM