0
    public class Test{

            static String symbol;

        public static void main (String[] args){

            String symbol = args[0];

            letter();

        }
        static void letter(){
            System.out.println(symbol);

        }
}

Why every time i try to run this the method letter successfully runs but prints Null? I just want to have one variable for all methods so that they can use its value.

TheSKDown
  • 13
  • 3
  • 1
    By declaring `String symbol`, you're declaring a new method-level reference. Use `symbol = args[0]` instead. – Zircon Oct 07 '16 at 19:32

1 Answers1

2

When you write String symbol = args[0]; in the main method you are declaring a new local variable, which shadows (hides) the static field called symbol. When you call letter that local variable is no longer applicable (since it is only applicable to main) and thus the value of the static field is printed, but that value is null since that static field was never assigned.

Change that line to symbol = args[0]; so it stores the value of args[0] to the static field instead.

nanofarad
  • 40,330
  • 4
  • 86
  • 117