首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

a question about class解决方法

2012-01-26 
a question about classpublicclassPair{intN_KeyStringN_Value}publicObjectinsert(){PairtmpParielem

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];

热点排行
Bad Request.