title: Callback to Class (non-instance)
body: <p>LWJGL 3 uses a callback system to handle inputs, like so:</p>
  
  <pre><code>public class Main
  {
  
      ...
  
      public static void main(String[] args)
      {
          glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback()
          {
              @Override
              public void invoke(long window, int key, int scancode, int action, int mods)
              {
  
              }
          });
      }
  
  }
  </code></pre>
  
  <p>However, I do not wish to be handling my inputs in the middle of code for another purpose.</p>
  
  <p>I can extend a class with GLFWKeyCallback and pass an instance of that class in to glfwSetKeyCallback(), however I am not executing that function from in an instance. It is running from a static function (main). I do not wish to move the code from main in to an instanced object since it conflicts with my coding style and that will stress me out and I'll cave and undo it eventually.</p>
  
  <p>The way I have it now is like so:</p>
  
  <pre><code>public class Main
  {
  
      private static GLFWKeyCallbackProxy glfwKeyCallbackProxy;
  
      public static void main(String[] args)
      {
          glfwSetKeyCallback(window, glfwKeyCallbackProxy);
      }
  
  }
  
  public class GLFWKeyCallbackProxy extends GLFWKeyCallback
  {
  
      @Override
      public void invoke(long window, int key, int scancode, int action, int mods)
      {
          // Communicate back to Main
      }
  
  }
  </code></pre>
  
  <p>I really dislike this also, as GLFWKeyCallbackProxy is now implementing part of the game, not just being used by the game. I believe it works against good OOP structure, and it has tight coupling.</p>
  
  <p>Ideally my solution would want to be somewhat equivalent to this hypothetical code:</p>
  
  <pre><code>public class Main extends GLFWKeyCallback
  {
  
      public static void main(String[] args)
      {
          glfwSetKeyCallback(window, ???);
      }
  
      @Override
      public static void invoke(long window, int key, int scancode, int action, int mods)
      {
          // This function now resides along side the code dealing with the rest of the game.
      }
  
  }
  </code></pre>
  
  <p>Essentially I want the callback functions to treat the static class level functions as though is was an instance, even though it isn't.</p>
  
  <p>How would you go about achieving this, or will I have to resort to some tight coupling like with my solution above?</p>
  
  <p>Edit: I forgot to mention, in whatever solution, I will need to have access to the internals of GLFWKeyCallback. Let's assume that I cannot access those internals purely by reference (through publics or getters/setters).</p>
  
  <p>Solution I ended up taking:</p>
  
  <pre><code>public class Game
  {
      static long window;
  
      public static void keyboardInvoke(long window, int key, int scancode, int action, int mods)
      {
  
      }
  
      public static void mouseInvoke(long window, int button, int action, int mods)
      {
  
      }
  
      public static void main(String[] args)
      {
          glfwSetKeyCallback(window, GLFWKeyCallback(Game::keyboardInvoke));
          glfwSetMouseButtonCallback(window, GLFWMouseButtonCallback(Game::mouseInvoke));
      }
  
  }
  </code></pre>