It is difficult to understand wjat you are asking, but here's a possible answer:
Make class B a subclass of A:
public class A {
// Declaration of the 'array' attribute
public float[] array = new float[]{1.1f, 2.2f, 3.3f};
}
class B extends A {
// Every instance of 'B' also has an 'array' attribute
}
If array
is redeclared to be public static
, you get a situation where there is an array
attribute that can be referred to as A.array
or B.array
. (Or within either A
or B
as just array
... or even as a.array
or b.array
where a
and b
have types A
and B
respectively.)
If you cannot create a direct or subtype relationship between A
and B
(or A
, 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:
public class A {
// Declaration of the 'array' attribute
public float[] array = new float[]{1.1f, 2.2f, 3.3f};
}
import static A.array;
class B {
// now I can use 'array' without qualifying it with 'A'
}
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.