Using GL class as a field. [newbie]

Is it good idea to use GL class as a field and initialize it only ones in init()
method instead of getting it for each time in methods display(), reshape()?

This:


class Test implements GLEventListener {
       GL gl;
      public void init(GLDrawable drawable) {
            gl = drawable.getGL();
            gl.doSomething();
      }

      public void reshape(
            GLDrawable drawable,
            int x,
            int y,
            int width,
            int height) {

            gl.doSomething();

      }

      public void displayChanged(
            GLDrawable drawable,
            boolean modeChanged,
            boolean deviceChanged) {
      }

       public void display(GLDrawable drawable) {
            gl.doSomething();
      }
}

Instead of this:


class Test implements GLEventListener {
       
      public void init(GLDrawable drawable) {
            GL gl = drawable.getGL();
            gl.doSomething();
      }

      public void reshape(
            GLDrawable drawable,
            int x,
            int y,
            int width,
            int height) {
            GL gl = drawable.getGL();
            gl.doSomething();
      }

      public void displayChanged(
            GLDrawable drawable,
            boolean modeChanged,
            boolean deviceChanged) {
      }

       public void display(GLDrawable drawable) {
            GL gl = drawable.getGL();
            gl.doSomething();
      }
}

No idea. Theoretically of course the second example will always be a slower than the first, the question is by how much. It might not matter much or it might matter a lot.
But shouldn’t showing the FPS in both cases tell you enough? Should be easy to set up a short example to test this and then come back and tell us what you found :wink:

I’ve already read that using GL class as a field is not recomended because it could make problems
with threading issues within the AWT and the dangers of making OpenGL calls from the wrong thread
(see JOGL User’s Guide:
https://jogl.dev.java.net/nonav/source/browse/checkout/jogl/doc/userguide/index.html?rev=HEAD&content-type=text/html)