Solved

assign final value based on System.property

Posted on 2013-01-11
10
270 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

914 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

19 Experts available now in Live!

Get 1:1 Help Now