无法在main方法中调用方法

时间:2013-10-18 09:44:09

标签: java

我正在尝试用另一种方法(比如在C#中)调用一个方法,如下所示:

public class Exercise1
{
   Scanner scanner = new Scanner(System.in);
   public int FirstNumber;
   public int SecondNumber;
   public int Answer;

   public static void main(String [] args)
   {
       GetNumbers();
   }

   private void GetNumbers()
   {
       System.out.print("Type in the first number: ");
       FirstNumber = scanner.nextInt();

       System.out.print("Type in the second number: ");
       SecondNumber = scanner.nextInt();

       Answer = FirstNumber + SecondNumber;

       System.out.print("The answer is: " + Answer);
   }
}

为什么我不能这样调用这个方法?

3 个答案:

答案 0 :(得分:7)

您无法在non static上下文中访问static个方法。

由于main方法是静态的,因此无法访问其中的非static方法。

可能的解决方案:

解决方案1。

GetNumbers();设为static。然后你就可以访问它了。

  private static void GetNumbers()
   {
      }

但是,在你的情况下,我不会推荐,因为你也在GetNumbers()方法中访问其他的instnace mebers。所以他们也需要static

解决方案2。

在main方法中为Exercise1类创建 new 对象。

  public static void main(String [] args)
   {
       Exercise1 ex= new Exercise1();
       ex.GetNumbers();
   }

   private  void GetNumbers()
   {
       System.out.print("Type in the first number: ");
       FirstNumber = scanner.nextInt();

       System.out.print("Type in the second number: ");
       SecondNumber = scanner.nextInt();

       Answer = FirstNumber + SecondNumber;

       System.out.print("The answer is: " + Answer);
   }

作为旁注:

请关注java naming conventions,带有小写字母的变量名称统计信息。

   public int firstNumber;
   public int secondNumber;
   public int answer;

答案 1 :(得分:4)

GetNumbers();需要static。静态方法只能调用static method或使用static fields

但是,如果object.GetNumber()的{​​{1}}实例是object的本地实例,则可以访问Exercise1。也就是说,在main()方法中创建Exercise1的实例对象,以访问该main()对象的Exercise1方法。

答案 2 :(得分:2)

main是静态方法,而GetNumbers()不是静态的 要么使GetNumbers()为静态,要么在main中创建一个对象,然后调用GetNumbers()

像这样

public class Exercise1
{
   Scanner scanner = new Scanner(System.in);
   public int FirstNumber;
   public int SecondNumber;
   public int Answer;

   public static void main(String [] args)

   {
Exercise1 e=new Exercise1();
       e.GetNumbers();
   }

   private void GetNumbers()
   {
       System.out.print("Type in the first number: ");
       FirstNumber = scanner.nextInt();

       System.out.print("Type in the second number: ");
       SecondNumber = scanner.nextInt();

       Answer = FirstNumber + SecondNumber;

       System.out.print("The answer is: " + Answer);
   }
}

    public class Exercise1
{
   static Scanner scanner = new Scanner(System.in);
   public static int FirstNumber;
   public static int SecondNumber;
   public static  int Answer;

   public static void main(String [] args)

   {
GetNumbers();
   }

   private static void GetNumbers()
   {
       System.out.print("Type in the first number: ");
       FirstNumber = scanner.nextInt();

       System.out.print("Type in the second number: ");
       SecondNumber = scanner.nextInt();

       Answer = FirstNumber + SecondNumber;

       System.out.print("The answer is: " + Answer);
   }
}