Results 1 to 2 of 2
Thread: Unchecked warning
- 10-22-2012, 10:36 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 14
- Rep Power
- 0
Unchecked warning
Hi All,
I didn't get what this warning message means and how to correct this.
I have following components and codes:Java Code:warning: [unchecked] unchecked conversion
a) Interface
b) Implementation ClassJava Code:public interface MyInterface { public List findAll(); }
c) Main ClassJava 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; } }
I am getting this warning in line 6 of Main Class. Can any one suggest how I can avoid this message?Java Code:public class MainClass { public static void main(String[] args) { MyClass test = new MyClass(); List<MyData> data = new ArrayList<MyData>(); data = test.findAll(); } }
Thanks
- 10-23-2012, 01:51 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
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
-
Generics and sort: "unchecked method invokation" compiler warning
By larole in forum New To JavaReplies: 3Last Post: 08-11-2012, 01:58 AM -
How to get rid of the unchecked or unsafe operations warning?
By yma16 in forum New To JavaReplies: 9Last Post: 05-11-2011, 04:31 PM -
unchecked exceptions
By veens4444 in forum New To JavaReplies: 1Last Post: 06-08-2010, 06:36 AM -
Unchecked or unsafe operations warning
By sky in forum New To JavaReplies: 3Last Post: 12-06-2009, 03:41 AM -
UnChecked Exception
By Java Tip in forum Java TipReplies: 0Last Post: 11-18-2007, 07:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks