Jogn Shall Jogn Shall - 21 days ago 4x
Java Question

Cant call a method in same java class

I haven't wrote java in ages, and I know my question is really simple, but I can't for the life of me figure out the error.

I'm trying to find the smallest number in a array, using the below code. The algorithm is right, but i get an error when trying to use it in the last print statement

package runtime;

import java.util.ArrayList;

public class app {

* @param args the command line arguments

public int findSmallElement(ArrayList<Integer> num)
int smElement;
smElement= num.get(0);
for(int i=0; i<num.size() ; i++)
if(num.get(i) < smElement)
return smElement;

public static void main(String[] args) {

ArrayList<Object> num = new ArrayList<Object>();


System.out.println("The size of the list is " +num.size());




You're trying to call your method on an ArrayList variable/object, which does not have this method, when instead you want to call it on an instance of your own class. You should pass your array list into this method.

Another option is to make your method static and simply call it by itself, again passing in the arraylist.

//  add the static modifier
public static int findSmallElement(ArrayList<Integer> num)

and then call like:

// pass the ArrayList into your findSmallElement method call
int smallestElement = findSmallElement(num);
// display the result:
System.out.println("smallest element: " + smallestElement);