Solved

assign final value based on System.property

Posted on 2013-01-11
10
273 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 166 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 16

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

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 167 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 167 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String array comparison 4 45
Java basic valueOf question 1 32
IBM TS2900 (3572) Tape Autoloader Java? 12 53
Java Inheritance super keyword use 8 30
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

685 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