当前位置: 代码迷 >> Java相关 >> 新手Java类中调用函数有关问题
  详细解决方案

新手Java类中调用函数有关问题

热度:66   发布时间:2016-04-22 19:30:03.0
新手Java类中调用函数问题


public class GetMax {

    //求数组最大值
    public int Max (int[]shuzu)
    {
     int temp=0;
     for(int i=0;i<=shuzu.length;i++)
     {
     if(shuzu[i]>temp)
     {
     temp=shuzu[i];
     }
     }
     return temp;
    }
    public static void main(String[]args)
    {
     int [] shuzu={2,5,8,9,5,6,12,1};
     int value=Max(shuzu);//这里为什么不能调用Max函数呢?
    }
}

------解决思路----------------------
在静态上下文中调用的方法必须也是静态的。在Max方法上加上static修饰符就行了
------解决思路----------------------
这属于基础知识了,main是static的,而Max函数时非静态的,他的存在与否要看,所在的类是否被实例化。向楼上说的一样,你这样编译都过不去。