so suppose I have a java package….
it’s got the main class with the main method
and then it’s got a whole bunch of other classes…..
my question is, is it possible to get the args that was passed into the main method from these other classes that are not part of the main class but in the same package…
No, not portably, there may be some trickery based on the JVM implementation but I’ve never seen it, and it would be a very bad idea to rely on it even if it existed.
If you want those values elsewhere, the
mainfunction needs to make them available somehow.An easy way to do this (not necessarily the best way) is to simply store away the strings as the first thing in
mainand provide a means for getting at them:Scratch2.java:
CmdLineArgs.java:
And, when run with the arguments
a b c, this outputs: