“方法main不能被声明为静态;静态方法只能在静态或顶级类型“ [英] "The method main cannot be declared static; static methods can only be declared in a static or top level type"

查看:2177
本文介绍了“方法main不能被声明为静态;静态方法只能在静态或顶级类型“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Perkusja {
boolean talerze = true;
boolean beben = true;

void zagrajNaBebnie(){
System.out.println(bam,bam,baaaa-am-am);
}
void zagrajNaTalerzach(){
System.out.println(brzdęk,brzbrzrzdęęk);
}
类PerkusjaTester {
public static void main(String [] args){
Perkusja p = new Perkusja();
}
}

}



您好!我是stackoverflow的新手,所以请原谅我我的残暴编辑。



我是Java的新手,我无法弄清楚问题在哪里,什么是问题。
我得到以下错误
public static void main(String [] args)


方法main不能声明为static;静态方法只能在静态或顶层类型中声明


我使用的是eclipse, 。我搜索问题,但答案通常涉及到更复杂的代码。



我如何解决它,以下是什么原因的错误?

解决方案



您在 PerkusjaTester 中声明了主方法,它是 Perkusja 的内部类。



您应该在 Perkusja之外声明您的测试类



注意: PerkusjaTester 是一个内部类,而不是一个静态嵌套类。这就是为什么 PerkusjaTester 不是静态类型。正如Jon Skeet所提到的,你还可以在类 PerkusjaTester 上添加关键字 static


class Perkusja {
boolean talerze = true;
boolean beben = true;

void zagrajNaBebnie() {
    System.out.println("bam, bam, baaaa-am-am");
}
void zagrajNaTalerzach() {
    System.out.println("brzdęk, brzbrzrzdęęk");
}
class PerkusjaTester {
    public static void main(String[] args) {
        Perkusja p = new Perkusja();
    }
}

}

Hello! I'm new to stackoverflow so please forgive me my atrocious editing.

I'm new to Java and i can't figure out where exactly the issue lies and what's the problem. I get the following error on the line public static void main(String[]args):

The method main cannot be declared static; static methods can only be declared in a static or top level type

I'm using eclipse and i'm doing some simple java exercises. I googled the problem but the answers are usually related to much more complex pieces of code.

How do i fix it and what's the cause of the following error? I'd be grateful for an explanation on this particular example.

Cheers!

解决方案

You are declaring your main method in PerkusjaTester which is an inner class of Perkusja. That is forbidden.

You should declare your test class outside of class Perkusja

Note : PerkusjaTester is an inner class, not a static nested class. That is why PerkusjaTester is not a static type. As mentionned by Jon Skeet, you could also add the keyword static on class PerkusjaTester.

这篇关于“方法main不能被声明为静态;静态方法只能在静态或顶级类型“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆