Variable might not have been initialized error

前端 未结 11 1575
孤城傲影
孤城傲影 2020-11-21 05:50

When i try to compile this:

public static Rand searchCount (int[] x) 
{
    int a ; 
    int b ; 

    ...   

    for (int l= 0; l

        
11条回答
  •  情歌与酒
    2020-11-21 06:07

    Local variables do not get default values. Their initial values are undefined with out assigning values by some means. Before you can use local variables they must be initialized.

    There is a big difference when you declare a variable at class level (as a member ie. as a field) and at method level.

    If you declare a field at class level they get default values according to their type. If you declare a variable at method level or as a block (means anycode inside {}) do not get any values and remain undefined until somehow they get some starting values ie some values assigned to them.

提交回复
热议问题