Results 1 to 7 of 7
Thread: package-private members
- 04-30-2011, 10:59 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
package-private members
It seems that you can access package-private members from outside a third-party package, by adding your own class to the package which would access the package-visible members of other classes and expose them via its own public emthods. E.g. you could define your own class to be in package java.util, which could then access package-private members of java.util classes and expose them. Is that accurate? Are there no restrictions on defining your own classes to be in third-party packages?
- 04-30-2011, 11:39 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
E.g. you could define your own class to be in package java.util, which could then access package-private members of java.util classes and expose them. Is that accurate?
Unless I'm missing something that sounds reasonable to me. After all that is exactly what the third party developers do all the time: add things to the package.
Have you searched java.util for a package private member and added your own class?
- 04-30-2011, 11:49 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
> what the third party developers do all the time: add things to the package.
I meant, what if _you_ add a class to _their_ package?
E.g. I had thought that if I create a package, say org.ilya, and have some
package-private members, then they cannot be read or written by anyone's code without going through my code.
But it seems that anyone can "parachute" their class into my package, and access package-private members that way. People normally don't do that of course; you don't normally write classes to be "parachuted into" java.util .
But it seems that this makes the package-private abstraction barrier optional rather than mandatory: anything exposed to the package can be read/written by any other program part directly. (In contrast to class-private members).
Is that correct?
- 05-01-2011, 01:12 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
I meant, what if _you_ add a class to _their_ package?
Yes, I understood that. But I simply could not see how the javac executable was supposed to figure out whether or not I who had typed the command to compile was employed by Company X who wrote the library.
I am not sure what you mean by "going through" code. If there is a package private member in org.ilya then any class added to the package (by you, by me, by anyone who can invoke the javac executable) can - by design - access the member.
Personally I wonder whether "package-private abstraction" is any sort of barrier at all.
-------------------
So, have you ripped open src.jar and found a suitable test in one of the classes of java.util?
- 05-01-2011, 06:10 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
- 05-02-2011, 06:59 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
- 05-03-2011, 09:59 AM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Similar Threads
-
Default / package-private access
By genkuro in forum New To JavaReplies: 1Last Post: 12-30-2010, 06:08 AM -
Ideas about private to private IP networking over the Internet
By mr_anderson in forum Advanced JavaReplies: 7Last Post: 06-28-2010, 07:52 AM -
run package inside anthor package
By AhmedAdel in forum AWT / SwingReplies: 4Last Post: 04-20-2010, 11:52 AM -
private data members?
By blueduiker in forum New To JavaReplies: 10Last Post: 01-19-2010, 11:13 AM -
The declared package does not match the expected package
By oneforall in forum EclipseReplies: 7Last Post: 11-09-2009, 07:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks