This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class CAst  Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Class CAst  Exception" Watch "Class CAst  Exception" New topic

Class CAst Exception

ramya ray
Ranch Hand

Joined: Aug 11, 2006
Posts: 101
It will be great help if some one please explain why the below code throwing Class Cast Exception:

import java.util.*;
public class Test1{
public static void main(String a[]){
Set s = new TreeSet();
s.add(new Person(20));
s.add(new Person(10));
class Person{
Person(int i){}

Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
A TreeSet requires that either the elements you insert into have to be mutually comparable, i.e. the class definition implements the Comparable interface, or that you create the TreeSet with a Comparator that specifies how two objects in the set will be compared.
ramya ray
Ranch Hand

Joined: Aug 11, 2006
Posts: 101
Thanks Keith ..i got it.
I agree. Here's the link:
subject: Class CAst Exception
Similar Threads
Why does it give exception
class cast exception
why ClasscastException????
why the output is classcastexception ?