-4

Possible Duplicate:
What is the reason behind “non-static method cannot be referenced from a static context”?

import java.io.*;

public class Pay
{
     boolean checkCard(int cardNumber)
    {
        boolean flag=false;
        if(cardNumber==12)  
        return flag;
    }

    public static void main(String args[])throws SQLException
        {
            boolean f=checkCard(12);
            System.out.println("\n Card Details "+f);
        }

}

Am getting error msg saying non-static method checkCard(int,String,String) cannot be referenced from a static context

Pls help me out

Community
  • 1
  • 1
udhaya kumar
  • 63
  • 1
  • 2
  • 6
  • 3
    Read about: ["Understanding Instance and Class Members"](http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) – Sujay Oct 27 '12 at 03:35
  • 1
    Because there aren't already hundreds of SO questions about this... – Matt Ball Oct 27 '12 at 03:39

3 Answers3

2
boolean checkCard(int cardNumber)
        {
            boolean flag=false;
            if(cardNumber==12)  
            return flag;
        }

is not static method. It is instance method.

But you are trying to access it from static method

public static void main(String args[])throws SQLException
            {
                boolean f=checkCard(12);
                 .....
}

To access instance method you need to instantiate the class and class this method on that instance.

Example:

new Pay().checkCard(12);

(or)

change the checkCard method signature to `static`.
kosa
  • 65,990
  • 13
  • 130
  • 167
0

Non-static method or variable can't be used directly(without reference) inside of static method.

Static method or variable can be used directly inside of static method.

public class Pay
    {
         static boolean checkCard(int cardNumber)
        {
            boolean flag=false;
            if(cardNumber==12)  
            return flag;
            else
               return boolean;---------missing
        }

        public static void main(String args[])throws SQLException
            {
                boolean f=checkCard(12);
                System.out.println("\n Card Details "+f);
            }

    }

Also you can create instance to call this

 public class Pay
        {
             boolean checkCard(int cardNumber)
            {
                boolean flag=false;
                if(cardNumber==12)  
                return flag;
                else
                   return boolean;---------missing
            }

            public static void main(String args[])throws SQLException
               {
                    Pay p= new Pay();
                    boolean f=p.checkCard(12);
                    System.out.println("\n Card Details "+f);
                }

        }
sunleo
  • 10,589
  • 35
  • 116
  • 196
0

You do not need to instantiate your Pay class. If indeed, you want the checkCard function to be static, you must indicate that the function is static:

static boolean checkCard(int cardNumber)
{
    boolean flag=false;
    if(cardNumber==12)
        flag = true;    // is this the behavior you want?
                        //  your original function had no
                        //  change to `flag` based on the condition.
    return flag;
}
Kyle Falconer
  • 8,302
  • 6
  • 48
  • 68