This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Posted by Matt Gerrans on November 14, 2001 at 1:42 PM
Don't do this! Trying to trick the compiler will only create headaches for you and others. It will also fail when a full rebuild is done.
How are you getting a reference to an object of that class upon which to call the method, anyway? You didn't say it was a static method...
If you need to access a method of a class which isn't public, here are some sensible ways that will not confuse the maintainers of the code:
- Make the class public. (you ruled this out, for some unknown reason).
- Make the class implement an interface that includes that method, then you can refer to the object with a reference of that interface type and call the method.
- Make the class extend a public abstract class that defines the method, much the same as the interface (using the interface would be better, though).
- Move the method to a class that is public in that package.
- Make a proxy method from a public class in the same package, so that it calls the innaccessible method for you.
> u can use one way to cheat it.
> first make the class(called A) in other package public,then compile ur program and run it,everything is ok.
> then modify A,remove the public symbol,recompile it.now everything is ok.u still can run the program.