0

I have the following:

public class ExampleObject extends GridObject { 
  private static Context c; 
  private static final String name = "Example Object"; 
  private static Bitmap skin = BitmapFactory.decodeResource(c.getResources(), R.drawable.defaultObject );
  private static float x,y; 

  public ExampleObject(Context c, float x, float y) { 
    this.c = c; 
    this.x = x; 
    this.y = y; 
  }
}

The class has 3 static class members, The image is a decoded bitmap, I want it to be decoded once and once only for use on ALL instances of this object.

In it's current state is this achieved? or is it decoded every time an instance of this class is created?

How should it be done?

2

3 Answers 3

2

A static field will only be initialized once; this is guaranteed by the JLS.

However, the decodeResource method will be called when the class is initialized, at which point your Context is null, so it will fail. You'll need something more complex if you want a static field which is only initialized once; something a bit closer to a singleton.

public class ExampleObject extends GridObject { 
  private static final String name = "Example Object"; 
  private static Bitmap skin;
  // = BitmapFactory.decodeResource(c.getResources(), R.drawable.defaultObject );
  private static float x,y; 

  public ExampleObject(Context c, float x, float y) { 
    synchronized(ExampleObject.class) {
      if(skin == null) {
        skin = BitmapFactory.decodeResource(c.getResources(), R.drawable.defaultObject);
      }
    }
    this.x = x; 
    this.y = y; 
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

public class ExampleObject extends GridObject { private static Context c; private static final String name = "Example Object"; private static Bitmap skin = BitmapFactory.decodeResource( c.getResources(), R.drawable.defaultObject ); private static float x,y; public ExampleObject(Context c, float x, float y) { this.c = c; this.x = x; this.y = y; } It's passed by constructor
so I would need to decode it within the constructor, but this would call it every time I create an instance of the object
Or will the JLS see that's it's static and already defined therefore ignore it?
You would need to check if the field was already assigned yourself. I will expand my answer with an example.
I could just store the r.drawable.defaultObject as an int and then decode in the onDraw method, however this decodes the image multiple times, exactly what I'm trying to avoid
1

You can achieve your intended behavior by the following class definition.

public class ExampleObject extends GridObject {
    private static Bitmap skin;

    public static Bitmap getSkin(Context c){
        if(skin == null){
            skin = BitmapFactory.decodeResource( c.getResources(), R.drawable.defaultObject );
        }
        return skin;
    }
}

2 Comments

perfect thanks, so trivial I'm such a fool sometimes! Thank-you very much much appreciated
A singleton is a better solution.
1

Static variables are initialized only once and A single copy of it is to be shared by all instances of the class.

This single initialization procedure is run automatically, one time only, when the class is first loaded.

You may want to use "static block" to initialize your classes's static fields. For example:

// start of static block 
    static {
        //initialize your static fields
        System.out.println("static block called ");
    }
    // end of static block 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.