Solved

assign final value based on System.property

Posted on 2013-01-11
10
271 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 21
Android development question 2 37
going to wrong jsp page 2 21
Java syntax, or is it Selenium 6 11
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

777 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