2012-10-27 6 views
-5
을 참조 할 수 없습니다

가능한 중복은 :
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); 
     } 

} 

오전 오류 비 정적 메소드 체크 카드를 말하는 MSG (INT, 문자열, String)를 얻는 것은 할 수 없습니다 정적 컨텍스트에서 참조해야합니다.

Pls help me out

+3

[ "인스턴스 및 클래스 멤버 이해"] (http://docs.oracle.com/ javase/tutorial/java/javaOO/classvars.html) – Sujay

+1

이것에 관해서는 이미 수백 가지의 질문이 없기 때문에 ... –

답변

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

은 정적 방법이 아닙니다. 인스턴스 메소드입니다.

그러나 당신은 당신이 클래스와 클래스 인스턴스에이 방법을 인스턴스화 할 필요가 인스턴스 메소드에 액세스하려면 정적 메소드

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

에서 액세스하려고합니다.

예 :

new Pay().checkCard(12); 

(또는)

change the checkCard method signature to `static`. 
0

비 정적 방식 또는 가변 정적 메소드 내부 (참조)없이 직접 사용할 수 없다.

정적 메서드 또는 변수를 정적 메서드 내부에서 직접 사용할 수 있습니다.

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); 
      } 

    } 

또한 당신은 당신은 당신의 지불 클래스를 인스턴스화 할 필요가 없습니다이

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); 
       } 

     } 
0

전화 인스턴스를 만들 수 있습니다. 실제로 checkCard 함수를 정적으로 지정하려면 함수가 정적임을 나타내야합니다.

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; 
} 
관련 문제