I'd like to pass a class variable to another class and make it a class variable of that class. How would I do this in the following context?
public class GLCamTest extends Activity {
public float array[] = something;
}
class GLLayer extends GLSurfaceView implements SurfaceHolder.Callback,
Camera.PreviewCallback, Renderer {
//Get class variable here
}
Do you want the array to be visible everywhere (as if it was a global variable)?
If so then it needs to be static.
But my guess is that you want to pass an instance of GLCamTest on to a GLLayer object, in which case you should use a setter function or pass it in the constructor.
It is difficult to understand wjat you are asking, but here's a possible answer:
Make class B a subclass of A:
If
array
is redeclared to bepublic static
, you get a situation where there is anarray
attribute that can be referred to asA.array
orB.array
. (Or within eitherA
orB
as justarray
... or even asa.array
orb.array
wherea
andb
have typesA
andB
respectively.)If you cannot create a direct or subtype relationship between
A
andB
(orA
,B
and some third class containing the declarations) then you are out of luck. There is no way that they can share declarations.However, you can use static imports to make it seem like the declaration is shared. For example:
Incidentally, it is generally a bad idea to use
static
variables to share state, especially state represented as bare arrays. This is distinctly non-object-oriented.Do you have access to instance of A? Or maybe you want array to be static?