Billy_wan 2015-10-07 03:03 采纳率: 66.7%
浏览 8436
已采纳

为什么这个会出现main不能声明为static呢?

package creat_class;

public class main_class
{
class box
{
double length;
double width;
double height;
void volume(){
System.out.print("volume is ");
System.out.println(length*width*height);
}
}

class boxdome
{
    public static void main(String arg[])
    /*The method main cannot be declared static; static methods can only be declared in a static or top level type*/
    {
    box mybox = new box();
    mybox.length = 9;
    mybox.width = 10;
    mybox.height = 8;

    mybox.volume();
    }
}

}

  • 写回答

4条回答 默认 最新

  • 毕小宝 领域专家: 后端开发技术领域 2015-10-07 06:05
    关注

    这个是java的基本语法,static的用法,只能在静态类型和顶层类型中定义静态方法。
    你的boxdome类是定义在main_class中的内部类型,所以不能定义static方法。
    另外:java类的定义都是遵循首字母大写的约定和规范的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?