Outsmarted myself - JOGL instance variables in subclasses.
Posted by LordSmoke on Jan 13, 2017; 5:56pm
URL: https://forum.jogamp.org/Outsmarted-myself-JOGL-instance-variables-in-subclasses-tp4037578.html
It seems I outsmarted myself. This is more a general Java question, but relevant to my current JOGL work...
I am creating a lineage of dialogs each implementing a single JOGL feature and inheriting the code from previous classes:
dialog01 draw white triangle on black background
dialog02 colored triangle on colored background
dialog03 animate rotation of triangle
dialog04 fix aspect ratio regardless of window shape
...
dialog04 extends dialog03 extends dialog02 extends dialog01 so that each dialog only contains more-or-less the code necessary to implement the new feature. Each dialog has its own vertex and fragment shader that extends those of the previous dialog.
This code is part of JavaDoc'ed and heavily commented code Java JOGL/OpenGL utility program I have been/am writing for my own use, but I am hoping it may serve as a useful "getting started" example.
Unfortunately, I just noticed (dialog04) that instanced variable values are shared in subclasses (1,2) - declare the background color in dialog01, change it in dialog04, and it changes for all open dialogs when they are redrawn.
I can hide this problem in my application by making the dialogs modal (won't be able to open any others), but is their a recommended solution for each subclass to have its own variable that is used by superclass methods?
I will want to change the geometry around dialog05 from a triangle to a cube, and expect I will run into this problem again with my vertexArray (dialog01), in addition to my bgColor and whatever else is lying around I have not tested.
(1) I can't believe I have not run into this problem before in all the 10s of thousands of lines and 100s of classes of Java code I have written. In thinking about it, I can see that I have never exploited the features of inheritance in my own code. I have written lots of unique java classes that only extend Java base classes and use their own variables internally.
(2) This also explains comments about not using an instanced glCanvas variable in response to and earlier question I posted. Thanks.