?
Solved

assign final value based on System.property

Posted on 2013-01-11
10
Medium Priority
?
279 Views
Last Modified: 2013-01-14
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.
0
Comment
Question by:allelopath
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 38768970
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

0
 
LVL 16

Assisted Solution

by:imladris
imladris earned 664 total points
ID: 38769025
#ifdef is normally used for "conditional compilation".
That is, they are directives for the compiler to insert the correct/relevant code into the object/executable depending on the intended environment.

So only the code for the intended environment is included in the executable. This will constrain its size but more importantly, allow the same source to be compiled by different compilers for different environments or machines depending on the settings. None of this is normally relevant to Java which runs on a virtual machine.

The most often used other objective is to differentiate between "debug" and "release" code, or something like that. Again, there is a size, and/or performance, issue here.

For this case, however, you could simply set a final variable in the source to specify what you want. Java will not compile anything inside blocks that can't be executed. So, if you write something like:

static final boolean DEBUG=true;

if(DEBUG)
{   check stuff, or log stuff or whatever
}

then that clause will be compiled if DEBUG is set to true, but it won't be compiled if you change DEBUG to be set to false. Just recompile that and then send it on its way.
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 38769032
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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Expert Comment

by:krakatoa
ID: 38769040
Sry CEHJ - my clocks are running slow. ;)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 38773976
>> 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

0
 
LVL 1

Author Comment

by:allelopath
ID: 38774366
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.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 668 total points
ID: 38774470
If you want a default value assigned even when the system property is not set, you could do the following (preferably with code more readable than mine ;))

public interface MyConstants {
    public static final float MY_CONSTANTS = (System.getProperty("myConstant") == null)
        ? 1.0f
        : (System.getProperty("myConstant").equalsIgnoreCase("2") ? 2.0f : 1.0f);
}

Open in new window

0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 668 total points
ID: 38774704
>> 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

OK. This is the updated code then:

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

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38774860
:)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 38774906
Thanx 4 axxepting
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question