0

Is it possible to access a method in different package but same project without making the method public or inheritance? I'm looking for something similar to friendly assemblies in C#.

Zigzagoon
  • 751
  • 5
  • 14

1 Answers1

3

Is it possible to access a method in different package but same project without making the method public or inheritance?

Sortof, yes - you can use reflection to get the method that you're after, call setAccessible(true) on the method to suppress the access checks, then invoke it from wherever you like.

But...

I'm looking for something similar to friendly assemblies in C#.

This isn't it, a similar feature simply doesn't exist in the Java language. The above reflection trick should really be treated as nothing more than a hack, and certainly not something you'd want to use in production code unless there really was no other feasible option.

Michael Berry
  • 61,291
  • 17
  • 134
  • 188