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");
}
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
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
So have you got "this far" then ? :
iface :
class MyCons implements MyConstants {
public static void main(String[] args){
System.out.println(MY_CONSTANTS);
}
}
iface :
public interface MyConstants {
public static final float MY_CONSTANTS = Float.parseFloat(
System.getProperty("java.version").equalsIgnoreCase("2") ? "2.0" : "1.0");
}
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:
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"));
}
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();
}
}
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:)
Thanx 4 axxepting
Open in new window