日期:2014-05-20  浏览次数:20583 次

a question about class
public   class   Pair   {
int   N_Key;
String   N_Value;
}

public   Object   insert()
{
Pair   tmp;
Pari   element;

if(element.N_key   ==   123){
/*****************************************
The   local   variable   tmp   may   not   have   been   initialized,why????????
************************/

tmp.N_key   =   element.N_key;
tmp.N_Value   =   element.N_Value;}
return   tmp;
}

------解决方案--------------------
Pair tmp=new Pair();
Pair element=new Pair();
------解决方案--------------------
要对tmp和element进行初始化。
Pair tmp=new Pair();
Pari element=new Pair();
------解决方案--------------------
If a class or a method or a variable is not define with the keyword "static ", it must be initialized before you use it.

PS:
------解决方案--------------------
public class Pair {
int N_Key;
String N_Value;
}

public Object insert()
{
Pair tmp; //java中类似表示,只是表示vm给该变量分配一个引用,而非产生对应得对象
Pari element;

if(element.N_key == 123){
/*****************************************
The local variable tmp may not have been initialized,why????????
************************/

tmp.N_key = element.N_key;
tmp.N_Value = element.N_Value;}
return tmp;
}


楼主应该是弄C++得吧..java中对对象得产生是需要new得,单纯得Pair tmp; 只是让虚拟机分配了一个引用,类似于c++里得 Pair* tmp ;

------解决方案--------------------
Pair[] element =new Pair[n];