The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001



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 Artima Forums.



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:

  1. Make the class public. (you ruled this out, for some unknown reason).
  2. 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.
  3. 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).
  4. Move the method to a class that is public in that package.
  5. Make a proxy method from a public class in the same package, so that it calls the innaccessible method for you.
  6. - mfg

    > 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 everything is ok.u still can run the program.


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us