Results 1 to 2 of 2
  1. #1
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    33
    Rep Power
    0

    Default Unchecked warning

    Hi All,

    I didn't get what this warning message means and how to correct this.
    Java Code:
    warning: [unchecked] unchecked conversion
    I have following components and codes:
    a) Interface
    Java Code:
    public interface MyInterface {
        
        public List findAll();
        
    }
    b) Implementation Class
    Java Code:
    public class MyClass implements MyInterface{
    
        public List findAll() {
            List<MyData> result = new ArrayList<MyData>();
            result.add(new MyData("1","ABC"));
            result.add(new MyData("12","DEF"));
            result.add(new MyData("13","GHI"));
            result.add(new MyData("14","JKL"));
            result.add(new MyData("15","MNO"));       
            return result;
        }
        
    }
    c) Main Class
    Java Code:
    public class MainClass {
        
        public static void main(String[] args) {
            MyClass test = new MyClass();
            List<MyData> data = new ArrayList<MyData>();
            data = test.findAll();        
        }    
    }
    I am getting this warning in line 6 of Main Class. Can any one suggest how I can avoid this message?

    Thanks

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Unchecked warning

    The interface MyInterface defines a method that returns a List, but there is no type definition of what the List contains (eg using Generics) - this means this method can return a List containing any type. In the main method, you call this method and attempt to set the returned results to a List defined as containing objects of type "MyData"...this present a problem because there is no guarantee the returned List contains objects of this type. Your code could run flawlessly, but Generics provide compile time checks such as this which can prevent runtime exceptions. You can either suppress the warning (not recommended), or somehow define the List type
    Lesson: Generics (Updated) (The Java™ Tutorials > Learning the Java Language)

Similar Threads

  1. Replies: 3
    Last Post: 08-11-2012, 01:58 AM
  2. Replies: 9
    Last Post: 05-11-2011, 04:31 PM
  3. unchecked exceptions
    By veens4444 in forum New To Java
    Replies: 1
    Last Post: 06-08-2010, 06:36 AM
  4. Unchecked or unsafe operations warning
    By sky in forum New To Java
    Replies: 3
    Last Post: 12-06-2009, 03:41 AM
  5. UnChecked Exception
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-18-2007, 07:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •