Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Runtime initialisation

Thread view: 
stixwix - 16 Feb 2006 18:19 GMT
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
Thomas Hawtin - 16 Feb 2006 20:26 GMT
> If I invoke the following code with 10 as the program argument, the
> output is:
[quoted text clipped - 9 lines]
>     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) {
>
[quoted text clipped - 3 lines]
>         System.out.println("scaleFactor="+scaleFactor);
>         System.out.println("NUMBER_X="+NUMBER_X);

              System.out.println("NUMBER_X="+getNumberX());

>     }
> }

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Rhino - 16 Feb 2006 20:27 GMT
> If I invoke the following code with 10 as the program argument, the
> output is:
[quoted text clipped - 19 lines]
> }
> }

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.

--
Rhino
Mike Schilling - 17 Feb 2006 00:31 GMT
> If I invoke the following code with 10 as the program argument, the
> output is:
[quoted text clipped - 19 lines]
> }
> }

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.




Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.