I'm trying to *adapt* a simulation I found, by passing command line arguments to the simulation to change the behaviour of the simulation, instead of manually changing the code by hand each time I want to change the simulation parameters.
The simulation has 4 classes involved from what I can tell: the main class, which calls a simulation setup class (which sets up the 3d environment), that calls a mammal behaviour class, that in turn extends an animal behaviour class. (I appreciate extends is no longer used -- old simulation obviously!) Now I need to get the arguments out of the main class and into either the mammal or animal behvaour class -- any suggestions?
If I pass the arguments, they need to go through the simulation setup, and in turn into the behaviour class, which seems messy. Can I set the arguments I'm interested in as constants in main (do global variables exist in Java?) and access them from the behaviour class?
Or can I create some kind of a getArgumentValue method in main, which the later classes can access? And if so, how do you reference the object created through the main method?
Hope this helps -- let me know if more info. is req'd!! Needless to say, I'm stumped, so any help you can provide would be of *great* help!
Case #1: Declare a instance variable to hold the value what you pass from the command line. Write a method to set the value of the instance variable.When the program runs call the method from main and pass the command line argument to the method and set the instance variable.
Hi Ron, In one class create instance variable,setter and getter method for that variable in setter method you take the command line argument and assign it to the variable. in getter method return the variable. Create other class in that class call the getter method you will get the value
Joined: Apr 26, 2007
Hi Ron, Look below
Case #1: Declare a instance variable to hold the value what you pass from the command line. Write a method to set the value of the instance variable.When the program runs call the method from main and pass the command line argument to the method and set the instance variable and write a getter method to get the value.
Something more or less Global does exist in Java - public static variables. (They are not quite global because you can load a class twice with two classloaders and get two copies of the variables, but it's about as close as we get.) This kind of near-global is as evil as any other global and we avoid it except for final variables that cannot be changed after they are initialized.
Passing several variables around can be a pain. Sometimes you can encapsulate them all in a single Context object and pass around just one thing. You can use final non-static variables in the Context and initialize them in a constructor. That's not terribly evil, but still a nuisance to pass around.
An "application assembler" is a neat approach. It instantiates the other objects that will be needed and initializes them from configuration or command line args.
This does some neat things to the design. The core class never does a new on the subsystem, so we can swap in alternative implementations of subsystem without changing the core class.
Any of those ideas help?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi