IamAndroida
IamAndroida

Reputation: 75

How to call java method on main class

I'm a beginner in Java and I have a very simple problem. 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.

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

Upvotes: 0

Views: 10446

Answers (7)

Zak
Zak

Reputation: 26

Firstly your method have an attribute which is "int[] nums " to call the method you need to set a value to your attribute . 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 );

put this line incide your Main it ill work

Upvotes: 0

Anil Reddy Yarragonda
Anil Reddy Yarragonda

Reputation: 767

Suppose if your class is there in com.arr package. 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. It is better to create object to your Array class along with package name.

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. we have to create Object to your class for calling the instance methods.

Upvotes: 2

Kyaw Zin Htun
Kyaw Zin Htun

Reputation: 1000

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

Upvotes: 1

babon
babon

Reputation: 3774

Or you could use reflection just for a change ;)

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

Upvotes: 0

Kana Ki
Kana Ki

Reputation: 401

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.

Your computeSum method is an instance method. Meaning that you need an instance of the object Array, to use it, and it will execute in that object's context.

Your choices:

1) Make computeSum static:

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:

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).

Hope this helps. :)

Upvotes: 0

Sandeep Kaul
Sandeep Kaul

Reputation: 3267

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:

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

Upvotes: 0

Sahid
Sahid

Reputation: 110

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

Upvotes: 0

Related Questions