Reputation: 35
Eclipse says I can't make a static reference to the non-static field Art.instance. Why does it think I'm calling Art.instance from a static context?
TDRenderer itself gets called like so:
renderer = new TDRenderer();
TDRenderer.java:
package towerDefense;
import java.awt.Graphics;
import java.awt.Image;
public class TDRenderer {
public Art art;
public TDRenderer()
{
art = Art.instance;
}
public void render(Graphics g)
{
for(int i = 0; i < 32; i++)
{
for(int j = 0; j < 24; j++)
{
Image itd = (Image)(art.sprites[art.level1.tiles[i][j].type]);
g.drawImage(itd, itd.getWidth(null), itd.getHeight(null), null);
}
}
}
}
Upvotes: 0
Views: 520
Reputation: 20783
public class TDRenderer {
public Art art;
public TDRenderer()
{
art = Art.instance;
}
//so on..
Assumption 1 - Art.instance
is a static
instance.
If so, you should declare your local variable art
of TDRenderer
as public static Art art;
Well then it is redundant and useless. Why don't you directly refer to Art.instance wherever you need it?
Upvotes: 0
Reputation: 364
You're calling the instance
property on the Art
class.
If it's not static (i.e. a class variable), then there is no value, as it is expecting to be referenced inside an object instantiated from the Art
class.
If you want a single value reference-able from anywhere Art
is imported, then put the static
prefix in front of the instance
declaration and provide a value for it in the Art
file. If you're looking to access the instance
variable from a specific Art
object, you need to create one and reference the instance
property of the created object.
Upvotes: 0
Reputation: 198043
It's not that you're in a static context; it's that instance
is not a static field of Art
, but referencing it as Art.instance
means you're trying to use it as if it were static.
Upvotes: 4