Alex Rivera | Logout

Outside classes accessing package-private methods

Asked 2010-05-21T06:25:28.277
13

Suppose I have a class in my package org.jake and it has a method with default access (no modifier). Then the method is visible inside the package only.

However, when someone receives the jar of my framework, what is to stop them from writing a new class, declaring its package as org.jake, and using my supposedly invisible method?

In other words, is there anything I can do to prevent this?

Edit
Report

2 Answers

6

There is nothing you can do to prevent this. Even private members can be accessed via reflection. You should consider the access modifiers in java to be merely suggestive.

answered 2010-05-21T06:28:04.707
0

I'd say simply do not allow them to run code where it can call yours, i.e. in the same JVM. You could instead consider offering only a (web)service they can call externally. I'm not very up to date on the best ways to implement this though.

answered 2010-05-21T10:23:29.430

Your Answer