0

I'm creating simple game engine in Java and I've got some packages like a:

  • Game
  • Input
  • Time
  • Graphics

Each package handles a lot of classes, most of them have (And should have) public access. Let's focus on one most important class called MouseInput.

MouseInput class handles ONLY public static methods like a getMousePosition(MouseAxis axis) {...} but it also handles some methods like a updateMousePosition() {...}.

And now I want to make this method (updateMousePosition()) callable ONLY by GameBase class that is inside Game package.

P.s. I don't want to put all those classes in one package! I want to separate them to don't make my project messy.

2th P.s. All those methods that I want to make callable only by GameBase are static.

skprime
  • 55
  • 8
  • Thats not possible. You can make them package-privat, but there is no concept of `friend` in Java (as there is in c++) – tkausl Jan 16 '16 at 14:43
  • But I know some libraries/applications that blocks access to some methods somehow. – skprime Jan 16 '16 at 14:47
  • They use package-private methods – tkausl Jan 16 '16 at 14:48
  • I said that I want to separate those classes. I know it's possible to achieve "friend" effect but not directly by telling Java to make another class friend, but it's very different solution. – skprime Jan 16 '16 at 14:50

1 Answers1

0

It is possible to restrict who can call even public method by checking the call stack. This adds some code to the method receiving the call but any "wrong" calls can be rejected.

Community
  • 1
  • 1
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93