my name is GYAN my name is GYAN - 5 months ago 10
Java Question

LinkedList does not recogize its methods when imported as java.lang.* in Ubuntu

I am using java 7 on Ubuntu 14.

$ java -version
java version "1.7.0_79"
OpenJDK Runtime Environment (IcedTea 2.5.6) (7u79-2.5.6-0ubuntu1.14.04.1)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)


I am writing a very simple code on LinkedList class as:

import java.util.*;
public class First
{
//@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args)
{
LinkedList ll = new LinkedList();
ll.add(5);
System.out.println(ll);
}
}


My compiler produces an error as:

$ javac First.java

First.java:8: error: cannot find symbol
ll.add(5);
^
symbol: method add(int)
location: variable ll of type LinkedList
1 error


But same code is perfectly working in Eclipse installed in same Ubuntu. And also working in windows as well.

If I import LinkedList class explicitly in Ubuntu, the code starts to work in ubuntu also:

import java.util.LinkedList;
public class First
{
//@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args)
{
LinkedList ll = new LinkedList();
ll.add(5);
System.out.println(ll);
}
}

$ javac First.java
Note: First.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
$ java First
[5]
$


In Ubuntu only to use ArrayList class it does not require to implement ArrayList explicitly.

import java.util.*;
public class First
{
//@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args)
{
ArrayList ll = new ArrayList();
ll.add(5);
System.out.println(ll);
}
}

$ javac First.java
Note: First.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
$ java First
[5]
$


I cannot understand what is the issue.

Answer

I think one of your class name LinkedList existed in you current directory where First.java is saved.

because what i see as a Console error

First.java:8: error: cannot find symbol   //method definition not found
            ll.add(5);
              ^
      symbol:   method add(int)
      location: variable ll of type LinkedList
    1 error

Here Method definition is not found but Class LinkedList definition is found by compiler and in that LinkedList Class there is no such method like add().

So, What I guess there must be exist on class named LinkedList in your Current directory. So, Compiler picks-up that class as a reference for this line.

 LinkedList ll = new LinkedList();

but no definition of add() method is found .

Thanks

Comments