I have two classes, and want to include a static instance of one class inside the other and access the static fields from the second class via the first.
This is so I can have non-identical instances with the same name.
Class A { public static package1.Foo foo; } Class B { public static package2.Foo foo; } //package1 Foo { public final static int bar = 1; } // package2 Foo { public final static int bar = 2; } // usage assertEquals(A.foo.bar, 1); assertEquals(B.foo.bar, 2);
This works, but I get a warning ‘The static field Foo.bar shoudl be accessed in a static way’. Can someone explain why this is and offer a ‘correct’ implementation.
I realize I could access the static instances directly, but if you have a long package hierarchy, that gets ugly:
assertEquals(net.FooCorp.divisions.A.package.Foo.bar, 1); assertEquals(net.FooCorp.divisions.B.package.Foo.bar, 2);
I agree with others that you’re probably thinking about this the wrong way. With that out of the way, this may work for you if you are only accessing static members: