TheSKDown TheSKDown - 16 days ago 5x
Java Question

Why I cant use the value of a static variable?

public class Test{

static String symbol;

public static void main (String[] args){

String symbol = args[0];


static void letter(){


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.


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.