0

I have a class where some variables are initialized with static final. This variables have a defined initial value that is used in it's same class, and in another class.

Now, I want the value of this variables to be dependant of a scale_factor:

From this:

class Ball {
    public static final float SPEED = 4.0f;
    //...

To this:

class Ball {
    public static final float SPEED = 4.0f * scale_factor;
    //...

Sopposing that scale_factor is another float with value from 1.0 to 3.0.

The problem is that if I do this, I get this error:

The field SPEED cannot be declared static in a non-static inner type, 
unless initialized with a constant expression

It suggest to remove the static modifier of SPEED. If I do it, then, I cannot use this variable in other classes cause it tells me to make it static to be able to use it.

UPDATE--

public class SinglePlayerView extends View {
    //...
    public static float scale_factor;
    //...

    public SinglePlayerView(Context context) {
        super(context);
        scale_factor = setScreenScale();
    }

    public float setScreenScale() {
        float scale = getResources().getDisplayMetrics().density;
        return scale;
    }



    class Ball {
        public float x, y, xp, yp, vx, vy;
        public float speed = SPEED;

        public static final double BOUND = Math.PI / 9;
        public static final float SPEED = 4.0f;
        public static final int RADIUS = 4;
        public static final double SALT = 4 * Math.PI / 9;

        public Ball() {

        }

        public Ball(Ball other) {
            x = other.x;
            y = other.y;
            xp = other.xp;
            yp = other.yp;
            vx = other.vx;
            vy = other.vy;
            speed = other.speed;
            mAngle = other.mAngle;
        }
        //...

The parameters I would need to multiply the scale_factor are SPEED and RADIUS

masmic
  • 3,526
  • 11
  • 52
  • 105
  • Just a correction: `some variables are initialized with static final` These **aren't variables**. The keyword `static` turns them into **constants** and can't be altered anymore. – Phantômaxx Apr 09 '14 at 10:41
  • @Vyger ok, understood. How can I change the value then, if I need to do it, but at the same time, it requests me them to be static? – masmic Apr 09 '14 at 10:42

3 Answers3

3

scale_factor must also be static and final and must appear before the definition of SPEED in the source file.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • I have `scale_factor` defined in another class as `static float`. I can't make it final, because it's value is not a constant, it depends on the screens density, I get it's value doing: `getResources().getDisplayMetrics().density;` – masmic Apr 09 '14 at 10:43
  • Please guys, any answer for my recent comment? – masmic Apr 09 '14 at 10:47
  • Your best bet is to have a function `getScaleFactor()` in the other class, that is multiplied by `SPEED` as appropriate. – Bathsheba Apr 09 '14 at 10:49
  • @Bathsheba could you provide an example of that? – masmic Apr 09 '14 at 10:49
  • In your other class: `public double getScaleFactor(){return 1.0;}`. In `Ball`, use `OtherClass.getScaleFactor() * SPEED`; – Bathsheba Apr 09 '14 at 10:51
  • Well, if i do that, it tells me to remove the final identifier, and if I remove it, tells me `The field SPEED cannot be declared static in a non-static inner type, unless initialized with a constant expressio` – masmic Apr 09 '14 at 10:55
  • Don't use my suggestion to initialise a variable but use it whenever you need the value. – Bathsheba Apr 09 '14 at 10:56
  • The problem is that there are several places where I use these variables. I would need some way of modifying it at the begining, so then It has the defined value – masmic Apr 09 '14 at 11:01
  • Can the result of `getResources().getDisplayMetrics().density;` change through the running of the program? – Bathsheba Apr 09 '14 at 11:09
  • No, this value gets at the begining the screen's density value (between 1.0 and 3.0). Depending on this value, I must make things bigger to fit devices with bigger screens. I'll update my post showing how I call it and how I get the value, the you can help me – masmic Apr 09 '14 at 11:11
  • It might be better to post as a new question. – Bathsheba Apr 09 '14 at 11:12
0

The following code works : In a ExampleA.java define

public static final float y = get();
public static final float SPEED = 4.0f * y ;
public static float get() {
        // TODO PROCESSiNG
        return 3;
}

In ExampleB.java use the constants ExampleA.y and ExampleA.SPEED

HTH

prstk
  • 14
  • 3
  • Ok, let's suppose I do it that way. Inside get() I have to do: `return float scale = getResources().getDisplayMetrics().density;` As `get()` is defined as static, doesn't let me call `getResources()` cause: `Cannot make a static reference to the non-static method getResources() from the type View` – masmic Apr 09 '14 at 11:23
  • In that case refer this link to call the non-static method getResources() in get() : http://stackoverflow.com/questions/2042813/calling-non-static-method-in-static-method-in-java like : public static float get() { // TODO return new ExampleB().getResources(); } – prstk Apr 09 '14 at 12:01
0

Finally I solved:

Just pasing the variables I need from Ball class to mainClass, and giving there the value I want. Then I work with them in the main class and I can call them from the Ball class.

masmic
  • 3,526
  • 11
  • 52
  • 105