How to use the static static variable to process arguments?

advertisements

So we've been assigned to create a program that contains 5 methods. The user needs to input a number on the terminal and that number will be the common variable that will be used by all the methods.

I know that

static int num = Integer.parseInt(args[0])

will be the line used to scan what number will be used. My problem comes from how exactly will I use said line. It gives me an error when I place it outside the main method and I cant use it for the non-main methods


static int num = Integer.parseInt(args[0])

You will get compile time error using this line because you cannot have a local static variable.

You should do this instead:

static int num;
public static void main([]args){
    num  = Integer.parseInt(args[0]);

   // Then use num where you want.
}