.\EvenOdd.java:16: error: non-static method CheckEven(int) cannot be referenced from a static context

EvenOdd,CheckEven
EvenOdd is the file name,it will change according to your file's name.CheckEven is the function name which will also vary.

Here is my code

public class EvenOdd {
    public static void main(String[] args) {
        int number = 10;
        if (CheckEven(number)) { // Error occurs here
            System.out.println("Even");
        } else {
            System.out.println("Odd");
        }
    }

    public void CheckEven(int num) { // This method is non-static
        if (num % 2 == 0) {
            return true;
        } else {
            return false;
        }
    }
}

What is this error?

The error message you provided indicates that you're trying to call a non-static method CheckEven(int) from a static context in your Java code. Here's what this error typically means and how you can resolve it:

In Java, when you declare a method as static, it means that the method belongs to the class itself rather than any specific instance of the class. Static methods can be called without creating an instance of the class.

On the other hand, non-static methods belong to instances of the class. They can only be called on objects (instances) of that class and not directly on the class itself.

To resolve this issue, you have a few options:

  1. Make the CheckEven method static

     public static boolean CheckEven(int num) {
         return num % 2 == 0;
     }
     // If CheckEven doesn't rely on any instance variables of the class,
     // you can declare it as static. 
     // This way, it can be called from a static context like the main method.
    
  2. Create an instance of the class and call the method on that instance

     public class EvenOdd {
         public static void main(String[] args) {
             EvenOdd obj = new EvenOdd();
             int number = 10;
             if (obj.CheckEven(number)) {
                 System.out.println("Even");
             } else {
                 System.out.println("Odd");
             }
         }
    
         public boolean CheckEven(int num) {
             return num % 2 == 0;
         }
     }
     //If CheckEven needs to access instance variables or other non-static 
     //members of the class,you'll need to create an instance of the class and
     //call the method on that instance.