Runtime initialisation

S

stixwix

If I invoke the following code with 10 as the program argument, the
output is:
scaleFactor = 10
NUMBER_X = 10

Is there a mechanism for forcing the runtime value to be used as it
seems a bit clunky to do it by hand (i.e., by calling a method to
update it).

public class Test {

private static int scaleFactor = 1;
private static int NUMBER_X = 10 * scaleFactor;

public static void main(String[] args) {

if(args.length == 1){
scaleFactor = Integer.parseInt(args[0]);
}
System.out.println("scaleFactor="+scaleFactor);
System.out.println("NUMBER_X="+NUMBER_X);
}
}

cheers,
Andy
 
R

Rhino

stixwix said:
If I invoke the following code with 10 as the program argument, the
output is:
scaleFactor = 10
NUMBER_X = 10

Is there a mechanism for forcing the runtime value to be used as it
seems a bit clunky to do it by hand (i.e., by calling a method to
update it).

public class Test {

private static int scaleFactor = 1;
private static int NUMBER_X = 10 * scaleFactor;

public static void main(String[] args) {

if(args.length == 1){
scaleFactor = Integer.parseInt(args[0]);
}
System.out.println("scaleFactor="+scaleFactor);
System.out.println("NUMBER_X="+NUMBER_X);
}
}
I've just read your post twice and I _still_ don't understand what you are
asking.

Do you want to pass a runtime parameter to a program and have the program
use it? If so, what's wrong with the way you're doing it in your example?
What do you not like about this approach?

There are other ways to get runtime parameters to programs but I'm not sure
if any of them is acceptable to you:
- you can prompt the user for values via a GUI/form
- you can indirectly set a runtime value outside of the program. For
example, I just wrote an Ant script that sets the value of a static final
constant before the program runs; I display a menu (via Ant) asking the user
which of various values the constant should have and Ant edits the class
source to set the constant to the desired value, then compiles and runs the
program.
- etc.

But the technique you use in your example is the simplest I can think of.
 
T

Thomas Hawtin

stixwix said:
If I invoke the following code with 10 as the program argument, the
output is:
scaleFactor = 10
NUMBER_X = 10

Is there a mechanism for forcing the runtime value to be used as it
seems a bit clunky to do it by hand (i.e., by calling a method to
update it).

public class Test {

private static int scaleFactor = 1;
private static int NUMBER_X = 10 * scaleFactor;

Replace that last line with:

private static int getNumberX() [
return 10 * scaleFactor;
}
public static void main(String[] args) {

if(args.length == 1){
scaleFactor = Integer.parseInt(args[0]);
}
System.out.println("scaleFactor="+scaleFactor);
System.out.println("NUMBER_X="+NUMBER_X);
System.out.println("NUMBER_X="+getNumberX());

}
}

Tom Hawtin
 
M

Mike Schilling

stixwix said:
If I invoke the following code with 10 as the program argument, the
output is:
scaleFactor = 10
NUMBER_X = 10

Is there a mechanism for forcing the runtime value to be used as it
seems a bit clunky to do it by hand (i.e., by calling a method to
update it).

public class Test {

private static int scaleFactor = 1;
private static int NUMBER_X = 10 * scaleFactor;

public static void main(String[] args) {

if(args.length == 1){
scaleFactor = Integer.parseInt(args[0]);
}
System.out.println("scaleFactor="+scaleFactor);
System.out.println("NUMBER_X="+NUMBER_X);
}
}

The issue is the order of execution of the statements in your program. The
static initializers run when the type is initialized, which happens before
any of the method run, that is:

1. First scaleFactor is set to 1
2. Next NUMBER_X is set to 10
3. Last scaleFactor is set to 10.

If you want NUMBER_X to depend on the runtime value of scaleFactor, simply
replace

private static int NUMBER_X = 10 * scaleFactor;

by

private static int NUMBER_X;

and add the line

NUMBER_X = 10 * scaleFactor;

after the close of the if statement.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top