Re: Command line arguments passed to main: how do I access them from other classes? ...



Lew wrote:
Mark Space wrote:
Ed wrote:

public class Test {
public static String[] args = null;

Initialization to null is entirely redundant.


public static void main(String[] args) {
this.args = args;
}
}

Then any other class can access them via:
String[] args = Main.args;


Just curious, but does this always work? (It's late and I'm fuzzy.) If class Main and the simulation are in different packages... hmm, you'd have to include the Main package in the code that's accessing...

import local.simulrun.Main;

//...

String [] args = Main.args;

Yeah ok, I guess it does, with a little help from import...

import is just a hint to the compiler to help it discern the package for a class. Essentially you always refer to every class by its fully-qualified name, implicitly or explicitly.

import "goes away" in the bytecode.

Of course, public static mutable members are a Bad Thing. Better to create an object and make the array an instance member with an appropriate accessor.

--
Lew
.



Relevant Pages

  • Re: main class not found in project (absurd error)
    ... public static void main(Stringargs) ... Is it then not possible to have a java console application return ... > The main package is called jtest for example. ... > thing it compiles but it just does not run at all!!!! ...
    (comp.lang.java.help)
  • Re: Bwidget Trees : Spaces in File Names
    ... using "args". ... + package require Itcl ... set fname $args ... > no such element in array ...
    (comp.lang.tcl)
  • Re: URGENT: Command argument parsing
    ... do you want this package to execute code or simply parse the arguments into some structure you'll traverse later? ... IMO you don't really need a package to do many kinds of argument parsing, it's quite easy to write your own argument processing and you won't be tied to limitations of whatever package you use. ... set args [lrange $args 1 end] ...
    (comp.lang.tcl)
  • Re: URGENT: Command argument parsing
    ... Abbreviating arguments is not ... I am looking for a package or a procedure/API because it will be used ... to execute code or simply parse the arguments into some structure you'll ... set args ...
    (comp.lang.tcl)
  • Problem with wishx start script from http://wiki.tcl.tk/207
    ... I'm beginner and have a problem to start some old wishx ... scripts. ... package require Tclx ... set args ...
    (comp.lang.tcl)