简体   繁体   English

如何在主类上调用Java方法

[英]How to call java method on main class

I'm a beginner in Java and I have a very simple problem. 我是Java的初学者,我有一个非常简单的问题。 I'm trying to finish an activity and I forgot how to call a method on the main class. 我正在尝试完成一项活动,却忘记了如何在主类上调用方法。

I keep getting an error whenever I try ways to call the computeSum method on the main class. 每当尝试在主类上调用computeSum方法的方法时,我都会不断收到错误消息。

Error: Error: Main method not found in class array.Array, please define the main method as: public static void main(String[] args)


public class Array{
    public static void main(String[] args ){
        //Dont know what to put here to call computeSum
    }

    public int computeSum(int[] nums){
        int sum = 0;
        for (int i=0; i<nums.length; i++){
            sum= sum+nums[i];
        }  
        return sum;    
    }
}

Suppose if your class is there in com.arr package. 假设您的班级在com.arr包中。 you can specify the qualified name of the class at the time of creating object. 您可以在创建对象时指定类的限定名称。 Because Array class is already available in java.util package. 因为Array类在java.util包中已经可用。 It is better to create object to your Array class along with package name. 最好与包装名称一起为Array类创建对象。

public static void main(String[] args){

             com.arr.Array a1 = new com.arr.Array();
             int[] numberArray = {1,7,9,0,45,2,89,47,3,-1,90,10,100};
              a1.computeSum(numberArray);
            }

computeSum() it's an instance method. computeSum()是一个实例方法。 we have to create Object to your class for calling the instance methods. 我们必须为您的类创建Object以便调用实例方法。

You can try this 你可以试试这个

public class Array{
public static void main(String[] args ){
    //Dont know what to put here to call computeSum
    int[] nums = {1,2,3,4,5};        
    int sum=computeSum(nums);
    System.out.println(sum);
}

public static int computeSum(int[] nums){
    int sum = 0;
    for (int i=0; i<nums.length; i++){
        sum= sum+nums[i];
    }  
    return sum;    
}
}

You need to create an instance to invoke member method of a class. 您需要创建一个实例来调用类的成员方法。

public static void main(String[] args ){
    Array myArray = new Array();
    int[] values = new int[] {1,2,3,4};
    myArray.computeSum(values);
}

First you need to understand the difference between static classes/members and instance classes/members. 首先,您需要了解静态类/成员与实例类/成员之间的区别。

Your main method is static - as is standard for the entry method of a program - meaning it is available immediately without any binding to an instance of the Array object. 您的main方法是静态的-这是程序进入方法的标准-意味着它可以立即使用,而无需绑定到Array对象的实例。

Your computeSum method is an instance method. 您的computeSum方法是一个实例方法。 Meaning that you need an instance of the object Array , to use it, and it will execute in that object's context. 这意味着您需要对象Array的实例才能使用它,它将在该对象的上下文中执行。

Your choices: 您的选择:

1) Make computeSum static: 1)使computeSum静态:

public static void main(String[] args) {
    computeSum({1,2,3});   // or Array.computeSum() outside of Array
}
public static int computeSum (int[] nums) {
   int sum = 0;
   for (int i=0; i<nums.length; i++){
       sum= sum+nums[i];
   }  
   return sum;    
}

2) Make an instance of the Array object: 2)制作Array对象的实例:

public static void main(String[] args){
    Array myArray = new Array();
    myArray().computeSum({1,2,3});
}
public static int computeSum (int[] nums) {
   int sum = 0;
   for (int i=0; i<nums.length; i++){
       sum= sum+nums[i];
   }  
   return sum;    
}

Static code - not ran in the context of an object's instance - can not reference members that are not static, this makes sense as, how would it know what object instance of that member you are referencing ( myArray1.computeSum() ? or myArray2.computeSum() ? It doesn't even know these two instances of the myArray object exist). 静态代码-不在对象实例的上下文中运行-无法引用非静态成员,这很有意义,因为它将如何知道您要引用的成员的哪个对象实例( myArray1.computeSum() ?或myArray2.computeSum() ?它甚至不知道myArray对象的这两个实例是否存在。

Hope this helps. 希望这可以帮助。 :) :)

You can read about instance methods and static methods. 您可以阅读有关实例方法和静态方法的信息。 computeSum() is an instance method, which means it would need an object of Class Array to be called, example: computeSum()是一个实例方法,这意味着它需要一个Class Array对象来调用,例如:

public static void main(String[] args){

     Array array = new Array();
     int[] nums = {1,2,3};
      array.computeSum(nums);
    }

Alternatively, you could make it a static method to use it without making an object, which is not recommended, but incase you want, this is how you can do it: 另外,您也可以将它设为不使用对象而使用它的静态方法,不建议这样做,但是如果您愿意,可以使用以下方法:

public class Array{
public static void main(String[] args ){

      int[] nums = {1,2,3};
      int sum = computeSum(nums);
}

public static int computeSum(int[] nums){
    int sum = 0;
    for (int i=0; i<nums.length; i++){
        sum= sum+nums[i];
    }  
    return sum;    
}
}

Or you could use reflection just for a change ;) 或者您可以将反射仅用于更改;)

https://docs.oracle.com/javase/tutorial/reflect/ https://docs.oracle.com/javase/tutorial/reflect/

Firstly your method have an attribute which is "int[] nums " to call the method you need to set a value to your attribute . 首先,您的方法具有一个“ int [] nums”属性来调用需要为属性设置值的方法。 NOTE that you don't have to give the same name to your attribute while calling. 注意,调用时不必为属性指定相同的名称。 for example : 1 - int[] Myattribute = {1,2,3}; int sum = computeSum(Myattribute ); 例如:1- int[] Myattribute = {1,2,3}; int sum = computeSum(Myattribute ); int[] Myattribute = {1,2,3}; int sum = computeSum(Myattribute );

put this line incide your Main it ill work 把这条线放在你主要的病

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM