Solved

assign final value based on System.property

Posted on 2013-01-11
10
269 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
Comment Utility
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
Comment Utility
#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
Comment Utility
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
 
LVL 16

Expert Comment

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

Expert Comment

by:zzynx
Comment Utility
>> 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:allelopath
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
:)
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Thanx 4 axxepting
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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:

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now