Link to home
Start Free TrialLog in
Avatar of allelopath
allelopath

asked on

assign final value based on System.property

I've got a feeling what I'm trying to do is not possible, but I'll ask. I have a final float in an Interface. I want the value of it to be assigned according to a system property.
public interface MyConstants {
    public static final float MY_CONSTANTS = Float.parseFloat(
    		System.getProperty("myConstant").equalsIgnoreCase("2") ? "2.0" : "1.0");
}

Open in new window

It must be this way, ie in an Interface and a final. So then set the property somewhere in the code:
System.setProperty("myConstant", "2"); // set to not 1

Open in new window

MY_CONSTANTS always ends up being 0.0. I would have at least expected to always to 1.0. I've got a feeling that this isn't possible because of when the class/interface loading occurs. Basically I'm trying to kluge a #ifdef sort of thing.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

That's fine. Example below. Bear in mind that the property must be set or you'll get an exception

public class Mc implements MyConstants {
    public Mc() {
        System.out.println(MY_CONSTANTS);
    }

    public static void main(String[] args) {
        new Mc();
    }
}

Open in new window

SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So have you got "this far" then ? :

class MyCons implements MyConstants {

public static void main(String[] args){

System.out.println(MY_CONSTANTS);

}

}

Open in new window


iface :

public interface MyConstants {
    public static final float MY_CONSTANTS = Float.parseFloat(
    		System.getProperty("java.version").equalsIgnoreCase("2") ? "2.0" : "1.0");
}

Open in new window

Sry CEHJ - my clocks are running slow. ;)
>> I want the value of it to be assigned according to a system property.
Why "according to"? Why not just simply assigning it the value of the property like this:

public interface MyConstants {
    public static final float MY_CONSTANTS = Float.parseFloat(System.getProperty("myConstant"));
}

Open in new window


public class MC implements MyConstants {
    public MC() {
        System.out.println(MY_CONSTANTS);
    }

    public static void main(String[] args) {
        System.setProperty("myConstant", "2.0"); // You probably won't set this here of course. Just to prove it works.
        new MC();
    }
}

Open in new window

Avatar of allelopath
allelopath

ASKER

imlandris:
Thanks for the explanation. So then, I'm not trying to emulate a #ifdef.

What I am trying to do is have MY_CONSTANTS have a default of 1 (if the property value is not specified) or get its valued from a system property if that system property is defined. My apologies to those who responded if I was not clear. In this case, the solution by zzynx is not what I need because a value must be specified for the system property.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:)
Thanx 4 axxepting