java-noobette
java-noobette

Reputation: 29

Calling other classes using command line arguments?

I am writing a program where I use 2 command line arguments - the first one to choose 1 of the 3 support classes, and the second would be the int input value.

This has me very confused on many levels, but the main thing I'd like to learn is how to refer to/call the support method from the App class.

So far, my plan is to: Use an if-else (that is, if args[0] = 1 then this&that; if args[0] = 2 then this&that, etc)

Am I on the right track? At the moment, I don't even know what "this&that" will be. I'm guessing it will be the statement where I call the other classes - how will I do that?

I am teaching myself, and it's really not easy :)

Thank you for your time and knowledge!

Upvotes: 1

Views: 180

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499730

If your support classes all just take an integer, you could make them all implement the same interface with an appropriate method. Then you could use any of various means to create an instance of the class based on the first command-line argument:

  • You could use Class.forName() and then Class.newInstance()
  • You could use a switch statement in Java 7
  • You could use if/else statements (remembering to check for equality using equals rather than ==

Once you've got an instance of some implementation of the interface, you can parse the second command line argument using Integer.parseInt or DecimalFormat, and then call the method on the interface.

Upvotes: 3

Related Questions