-1

I'm a beginner programmer, I'm currently attending classes for programming in Java (and other things, it's an IT course). I'm currently copying some basic code for a very basic program from my notebook to NetBeans 8.1.

I made sure the code is EXACTLY as we did in class, I have a photo of the board, I've seen lots of people with this error here and it almost always seems like a few extra braces or these being put somewhere else they shouldn't be in.

I tried removing and adding and changing place but nothing works. This is my code:

public static int Mayor (int a, int b){
       if (a > b) {
           return a;
       }else{
           if (a == b){
               return -1;
          }else{
              return b;
          }
        }
}

Any help would be appreciated, thanks.

Moonfrost
  • 9
  • 1
  • Methods must be declared inside classes. – Andreas Sep 15 '18 at 20:06
  • Java methods must be declared inside a class (as the error message says). Slides don't always display the whole code, but concentrate on the important part. You need to learn the syntax of the language. Read a Java book, and/or the Java tutorial. – JB Nizet Sep 15 '18 at 20:06
  • That said, if you indeed pasted that verbatim, your teacher doesn't even respect the basic Java naming conventions, which is not a good sign. – JB Nizet Sep 15 '18 at 20:07
  • Duplicate of [Compiler error: “class, interface, or enum expected”](https://stackoverflow.com/q/12749994/5221149) – Andreas Sep 15 '18 at 20:10
  • For some additional reading you should take a look at [Java Code Conventions](http://www.oracle.com/technetwork/java/codeconventions-135099.html). A method name should not begin with a capital letter. – D.B. Sep 16 '18 at 00:16

2 Answers2

2

Java does not have standalone methods. You need to wrap your function inside a class.

Markus
  • 3,155
  • 2
  • 23
  • 33
1

an example to Markus'es answer:

public class calss1 {
   public static int Mayor (int a, int b){
          if (a > b) {
              return a;
          }else{
              if (a == b){
                  return -1;
              }else{
                 return b;
              }
          }
    }

}
javajav
  • 379
  • 3
  • 10