Re: Command line arguments passed to main: how do I access them from other classes? ...
- From: Lew <lew@xxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 26 May 2007 10:47:27 -0400
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
.
- Follow-Ups:
- References:
- Command line arguments passed to main: how do I access them from other classes? ...
- From: donkeyboy
- Re: Command line arguments passed to main: how do I access them from other classes? ...
- From: Ed
- Re: Command line arguments passed to main: how do I access them from other classes? ...
- From: Mark Space
- Re: Command line arguments passed to main: how do I access them from other classes? ...
- From: Lew
- Command line arguments passed to main: how do I access them from other classes? ...
- Prev by Date: Re: Command line arguments passed to main: how do I access them from other classes? ...
- Next by Date: Bug in code from Java Tutorial or is it just me?
- Previous by thread: Re: Command line arguments passed to main: how do I access them from other classes? ...
- Next by thread: Re: Command line arguments passed to main: how do I access them from other classes? ...
- Index(es):
Relevant Pages
|
|