Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 787
Lotix Avatar asked Oct 24 '25 04:10

Lotix


1 Answers

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.

like image 142
Arnaud Denoyelle Avatar answered Oct 27 '25 01:10

Arnaud Denoyelle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!