Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3980340
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:16:45+00:00 2026-05-20T05:16:45+00:00

so suppose I have a java package…. it’s got the main class with the

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:16:45+00:00Added an answer on May 20, 2026 at 5:16 am

    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 main function 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 main and provide a means for getting at them:

    Scratch2.java:

    public class Scratch2 {
        // Arguments and accessor for them.
    
        private static String[] savedArgs;
        public static String[] getArgs() {
            return savedArgs;
        }
    
        public static void main(String[] args) {
            // Save them away for later.
    
            savedArgs = args;
    
            // Test that other classes can get them.
    
            CmdLineArgs cla = new CmdLineArgs();
            cla.printArgs();
        }
    }
    

    CmdLineArgs.java:

    public class CmdLineArgs {
        public void printArgs() {
            String[] args = Scratch2.getArgs();
            System.out.println ("Arg count is [" + args.length + "]");
            for (int i = 0; i < args.length; i++) {
                System.out.println ("Arg[" + i + "] is [" + args[i] + "]");
            }
        }
    }
    

    And, when run with the arguments a b c, this outputs:

    Arg count is [3]
    Arg[0] is [a]
    Arg[1] is [b]
    Arg[2] is [c]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that I have a Java class with a static method, like so: class
Suppose I have a Java method that returns a HashMap object. Because a LinkedHashMap
Say suppose I have the following Java code. public class Example { public static
In Java, suppose I have 3 classes, C extends from B which extends from
Suppose, I have a lot of classes, which are constructed using Java reflection (for
Suppose I have the following Java file in a library: package test; public abstract
Suppose you have a Java method void foobar(int id, String ... args) and want
Suppose we have the Java code: Object arr = Array.newInstance(Array.class, 5); Would that run?
Suppose that I have a Java program within an IDE (Eclipse in this case).
I have two questions about java.awt.Shape . Suppose I have two Shape s, shape1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.