Cant call method from another class?

Ummayair Ahmad :

So I'm practicing with linked lists and I have three classes one Node class, one LinkedList class, and another class that I just have a main to test with.

In my LinkedList class I have an insert method, but when I try to call the insert method in my class with the main it won't recognize it and says Cannot resolve method insert(int)

Heres the code for my Node class

public class Node {
int data;
Node next;

}

public class LinkedList {

Node top;

public void insert(int data){
     Node node = new Node();
     node.data = data;
     node.next = null;

     if(top == null){       
         top = node;
     }
     else{
         Node n = top;   
         while (n.next != null ){ 
             n = n.next; 

         }
         n.next = node;
     }
}

Heres my main, where I try to call the insert method but it won't let me.

public class Runner {


public static void main(String [] args){

    LinkedList list = new LinkedList();
    list.insert(5);

}
Aniket Sahrawat :

You are seeing this because you have imported java.util.LinkedList in place of your custom made LinkedList class.

You can either import your LinkedList class and remove the import from java.util.LinkedList or use a fully qualified name insted of LinkedList. Eg:

package.of.custom.LinkedList list = new package.of.custom.LinkedList();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=105761&siteId=1