-1

Hi I have this piece of code and i am really confused to why i have to make the lel method static.The error is this "non static method cant be referred from static content". Usually when I create methods either to construct new objects or to manipulate objects in the main method I do not get this error message.Plus, i never declared e to be static!!. can someone please explain to me why this occurring?? Thank you :)

class x {

    public static void main(String[]args){

        int e= 2232;

        e= lel(e);

    }

    int lel(int k){
        return k+1;
    }
}
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38

1 Answers1

0

There are two solutions you could implement. The first option is to make your int lel(int k) a static method which would look like static int lel(int k)

Your other option is to declare a new object of your class x and use that for your lel method within main as MickMnemonic suggested in the comments. That code would look like:

e = new x().lel(e);

I believe the simplest thing would be to make the lel method static but it is up to you.

A deeper explanation of static methods can be found here.

Matt
  • 140
  • 1
  • 3
  • 8
  • 1
    'Any code referenced within the main method must also be static' is simply untrue. Otherwise there could never be any non-static methods. Your own suggestion proves the point. – user207421 Jul 25 '15 at 01:20
  • I was not thinking right when I wrote that, thank you for pointing it out, I removed it from my answer. – Matt Jul 25 '15 at 01:23